在更新我的应用程序以支持后台应用程序刷新时,我遇到了 AFNetworking 问题。
我正进入(状态NSPOSIXErrorDomain Code=53 "Software caused connection abort"
。该问题似乎出现在 iOS 12 中,后台连接被终止。
AFNetworking 2.6.3 用于进行获取。
AppDelegate.m
:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[OrdersService performFetch];
completionHandler(UIBackgroundFetchResultNewData);
}
OrdersService.m
:
-(void) performFetch {
[[AFHTTPRequestOperationManager new] GET:@"https://www.example.com/orders"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}
];
}
控制台输出:
[错误] GET '(null)' (0) [31.9163 s]: 错误域=NSPOSIXErrorDomain
代码=53“软件导致连接中止”
用户信息={NSErrorFailingURLStringKey=https://www.example.com/orders https://www.example.com/orders,
_kCFStreamErrorDomainKey = 1,NSErrorPeerAddressKey = {长度= 16,容量= 16,字节=
0x100201bb3e80187c0000000000000000},_kCFStreamErrorCodeKey = 53,
NSErrorFailingURLKey=https://www.example.com/orders https://www.example.com/orders}
以 0.1 秒延迟启动 fetch 作为后台任务解决了这个问题:
-(void) performFetch {
__block UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"GET /orders" expirationHandler:^{
// EXPIRED
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[[AFHTTPRequestOperationManager new] GET:@"https://www.example.com/orders"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// SUCCESS
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// FAILURE
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
];
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)