iosbluetoothbackgroundafnetworkingbftask

Freezing http requests in background state


There app have many tasks when app in background state, basically work with bluetooth dongles, and receive/sending data to server. Sometimes when app in background state send a query, system is "freeze in" this query. And after launch app, system is "freeze out", and we have

Error Domain=kCFErrorDomainCFNetwork Code=-1001 "The request timed out.

App use last AFNetworking 3.1.0 at this moment, and BFTask helper.

For more detail. Imagine, user use app for some reason, after app goes app to background state (user press home button or lock device), app normally continue working at background state. System or user can kill the app. App can wake up after some user interaction (via geolocation services, geofencing) at wake up moment, app is restarting, and send query to sever, with checking user (authorization, gets new token etc). At this moment we can see in the logs:

> Request method: POST url: https://******/v1/account/token headers: {
    UserName = "***@***.com";
    "grant_type" = password;
    password = ****
} at time: 2016-08-15 15:30:45 +0000

code:

    if ([request.method isEqualToString:@"GET"]) {
            return [manager GET:request.url parameters:request.params progress:nil success:^(NSURLSessionTask *operation, id responseObject) {

            } failure:^(NSURLSessionTask *operation, NSError *error) {

                    processError(operation, error);
                }
            }];

after some period of time user launch app, and this code trying to continue executing, and we have error with timeout.

    error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x156d8ce10
{Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" 
UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://******/v1/account/token, NSErrorFailingURLKey=https://******/v1/account/token, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.} 
at time: 2016-08-15 15:46:28 +0000

The difference in time about 16 minutes, when user launch app again. And this can be in another part of http queries

The question is, why iOS freeze queries in background, and how to fix this.


Solution

  • After some time with different experiments with network, finally I found issue. After enabled CFNetwork diagnostic, we can get more info about all requests in app. And CFNetwork get not -1001 error, instead all errors mark as Code=-1005 “null”, but -1005 error is "The network connection was lost". I don't know why AFNetworking got it this error, but this make sense. Issue begins when we connected to Wi-Fi or 3G/4G and don't have internet connection (only local), and at this moment app trying to do requests, CFNetwork alert -1005 error at this moment, but AFNetworking return error with -1001 code. That is issue of my topic question.

    At this moment, I have not elegant solution for this, before every request trying to ping host, and if we have answer do it request. Maybe u know better way?