AFNetworking是否有后台支持? [英] Does AFNetworking have backgrounding support?
问题描述
我正在调查 AFNetworking
作为 ASIHTTPRequest
的替代品,并注意完全没有有关它是否支持后台下载/上传的信息。
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.
使用 ASIHTTPReqeust
对象,您所要做的就是调用 [request setShouldContinueWhenAppEntersBackground:YES]
,请求将在后台继续。在 AFNetworking
中是否有任何支持?
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
?
推荐答案
编辑:从 AFNetworking 1.0RC1 开始,这是显式功能。 AFURLConnectionOperation
现在有方法 setShouldExecuteAsBackgroundTaskWithExpirationHandler:
,它可以为您透明地管理所有这些。
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.
这是一个隐含的功能,所以我并没有考虑做广告。您需要做的就是:
- (void)applicationWillResignActive:(UIApplication *)application {
__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
[application endBackgroundTask:backgroundTaskIdentifier];
[[YourRestClient sharedClient] cancelAllHTTPOperations];
}];
}
或者,如果您管理自己的运营拥有 NSOperationQueue
,只需 -cancelAllOperations
。
Or, if you manage your operations in your own NSOperationQueue
, just -cancelAllOperations
here instead.
这篇关于AFNetworking是否有后台支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!