尝试后台获取期间出现AFNetworking错误53 [英] AFNetworking error 53 during attempted background fetch
问题描述
在更新应用程序以支持后台应用程序刷新时,我遇到了AFNetworking问题。
While updating my application to support Background App Refresh I ran into problem with AFNetworking.
我正在获取 NSPOSIXErrorDomain代码= 53软件导致连接中止
。该问题似乎发生在iOS 12中,其中的后台连接被终止。
I am getting NSPOSIXErrorDomain Code=53 "Software caused connection abort"
. The problem seems to occur in iOS 12, where the background connection gets terminated.
AFNetworking 2.6.3用于进行抓取。
AFNetworking 2.6.3 is used to make the fetch.
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软件导致连接中止
UserInfo = { NSErrorFailingURLStringKey = https://www.example.com/orders ,
_kCFStreamErrorDomainKey = 1,NSErrorPeerAddressKey = {长度= 16,容量= 16,字节=
0x100201bb3e80187c0000000000000000},_ kCFStreamErrorCodeKey = 53,
NSErrorFailingURLKey = https://www.example.com/orders }
[Error] GET '(null)' (0) [31.9163 s]: Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort" UserInfo={NSErrorFailingURLStringKey=https://www.example.com/orders, _kCFStreamErrorDomainKey=1, NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bb3e80187c0000000000000000}, _kCFStreamErrorCodeKey=53, NSErrorFailingURLKey=https://www.example.com/orders}
推荐答案
以0.1秒的延迟启动作为后台任务的抓取解决了以下问题:
Starting the fetch as background task with 0.1 sec delay solved the problem:
-(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;
}
];
});
}
这篇关于尝试后台获取期间出现AFNetworking错误53的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!