Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm in the process of investigating AFNetworking as a replacement for ASIHTTPRequest, and notice a complete lack of information on whether it supports background downloads/uploads.

With an ASIHTTPReqeust object, all you have to do is call [request setShouldContinueWhenAppEntersBackground:YES] and the request will continue in the background. Is there any support for this in AFNetworking?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
345 views
Welcome To Ask or Share your Answers For Others

1 Answer

EDIT: As of AFNetworking 1.0RC1, this is an explicit feature. AFURLConnectionOperation now has the method setShouldExecuteAsBackgroundTaskWithExpirationHandler:, which transparently manages all of this for you.


It's an implicit feature, so I didn't really think about advertising it. All you'd need to do is:


- (void)applicationWillResignActive:(UIApplication *)application {
    __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
        [application endBackgroundTask:backgroundTaskIdentifier];
        [[YourRestClient sharedClient] cancelAllHTTPOperations];
    }];
}

Or, if you manage your operations in your own NSOperationQueue, just -cancelAllOperations here instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...