Reputation: 919
Alamofire give me an error when I call API and Lock mobile screen or Minimize application.
"The operation couldn't be completed. Software caused connection abort"
Please give me a solution for this issue.
Upvotes: 3
Views: 6765
Reputation: 1442
Here's an answer to the same question on Alamofire's GitHub page that I find useful:
[...] this is an underlying system error that can happen when network requests are killed during backgrounding. I suggest you implement background tasks around your network requests to keep them alive long enough to complete while in the background. You can find thorough documentation on Apple's dev forums. Once you properly handle that transition, the errors should stop, or give you a chance to properly cancel the requests.[
Upvotes: 1
Reputation: 919
I found an accurate solution for this problem:
AppDelegate:
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
APIManager.shared.backgroundCompletionHandler = completionHandler
}
In Your API Manager Class:
private lazy var backgroundManager: Alamofire.SessionManager = {
let bundleIdentifier = "com.YourDomain.YourAppName"
return Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: bundleIdentifier + ".background"))
}()
var backgroundCompletionHandler: (() -> Void)? {
get {
return backgroundManager.backgroundCompletionHandler
}
set {
backgroundManager.backgroundCompletionHandler = newValue
}
}
How to Call API:
let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: nil)
backgroundManager.session.configuration.shouldUseExtendedBackgroundIdleMode = true
backgroundManager.startRequestsImmediately = true
backgroundManager.session.configuration.timeoutIntervalForRequest = 180
backgroundManager.request(url, method: apiType, parameters: dict, encoding: URLEncoding.default, headers: headers)
.responseJSON(queue: .main) { (response: DataResponse<Any>) in
UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
switch(response.result) {
case .success(_):
case .failure(_):
}
}
Upvotes: 0
Reputation: 294
I think that iOS 12 close the connection before the last request returns a result when the app goes in background. you can use the following code to resolve your issue:
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];
}
}
Upvotes: 3