George L
George L

Reputation: 1738

Sending GET request using AFNetworking 2.0 on applicationDidEnterBackground

I am trying to download a small file using AFNetworking 2.0 when entering the background. For whatever reason neither NSLog appears in the console.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:@"SOMEURLHIDDEN" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error);
}];

Upvotes: 0

Views: 553

Answers (2)

Gad
Gad

Reputation: 2877

Here's something you should consider (taken from this StackOverflow question):

AFHTTPRequestOperationManager uses the old NSURLConnection so that doesn't facilitate background downloading.

AFURLSessionManager uses NSURLSession under the hood so that does

That being said, if for some reason you insist on using the AFHTTPRequestOperationManager keep on reading.

If you are trying to run this code in applicationDidEnterBackground: or similar then it's understandable why you're not seeing anything in the console. When the application enters the background it can be put in a suspended state by the OS at any time which is probably what is happening in your case (read more about App States). If you need more time to run code then you must explicitly ask for it by calling:

__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{}

This should be placed just before the code you wish to run in the background.

The expiration handler is called once your app has run out of time and is going to be suspended. It is important to finish up whatever you were doing (in your case you should cancel the GET request if it has not completed) and call:

[application endBackgroundTask:backgroundTaskIdentifier]

All in all your code should look something like this:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    __block AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [manager.operationQueue cancelAllOperations];
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier];
    }];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:@"SOMEURLHIDDEN" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

Upvotes: 3

liaogang
liaogang

Reputation: 518

I find in SDWebimage library.

#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
    if ([self shouldContinueWhenAppEntersBackground]) {
        __weak __typeof__ (self) wself = self;
        self.backgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            __strong __typeof (wself) sself = wself;

            if (sself) {
                [sself cancel];

                [[UIApplication sharedApplication] endBackgroundTask:sself.backgroundTaskId];
                sself.backgroundTaskId = UIBackgroundTaskInvalid;
            }
        }];
    }
#endif

Upvotes: 0

Related Questions