Alamofire错误:操作无法完成。软件导致连接中止

问题描述 投票:1回答:1

当我调用API和锁定移动屏幕或最小化应用程序时,Alamofire会给我一个错误。

“操作无法完成。软件导致连接中止”

enter image description here

  • 我的API需要60秒才能完成操作。
  • 这只发生在iPhone中,而不是模拟器中。

请给我一个解决这个问题的方法。

ios iphone swift3 alamofire
1个回答
2
投票

我认为iOS 12在应用进入后台时,在最后一个请求返回结果之前关闭连接。您可以使用以下代码来解决您的问题:

if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
    [self sendBackgroundDataToServer];

}

- (void) sendBackgroundDataToServer {
         UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
         bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
             [[UIApplication sharedApplication] endBackgroundTask:bgTask];
         }];

         NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:2];
        [dictionary setObject:[NSNumber numberWithDouble:lc.coordinate.latitude] forKey:@"floLatitude"];
        [dictionary setObject:[NSNumber numberWithDouble:lc.coordinate.longitude] forKey:@"floLongitude"];
        // send to server with a synchronous request


       // AFTER ALL THE UPDATES, close the task
       if (bgTask != UIBackgroundTaskInvalid) {
           [[UIApplication sharedApplication] endBackgroundTask:bgTask];
       }
}
© www.soinside.com 2019 - 2024. All rights reserved.