ios - 尝试后台获取期间出现 AFNetworking 错误 53

标签 ios afnetworking afnetworking-2 ios12 afhttprequestoperation

在更新我的应用程序以支持后台应用程序刷新时,我遇到了 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) {

           }
    ];
}

控制台输出:

[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 秒的延迟启 Action 为后台任务的抓取解决了问题:

-(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;
                           }
        ];
    });
}

关于ios - 尝试后台获取期间出现 AFNetworking 错误 53,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53297188/

相关文章:

ios - 在 iOS 中从 HLS 下载特​​定分辨率

ios - NSOperation 队列行为异常

ios - 将核心数据与服务器响应同步

ios - AFNetworking 2.0.0-RC2 : No visible @interface for 'DNHackerNewsAPIClient' declares the selector 'getPath:parameters:success:failure:' ?

ios - JSON 解码器 Swift 的潜在错误

ios - 从自定义按钮执行 Segue

ios - 使用存储的 NSUserDefaults 登录网站

ios - 应该如何使用 showAlertViewForRequestOperationWithErrorOnCompletion?

ios - iOS App 分发

ios - 使用AFNetworking _convertJSONString保持分配状态