AFNetworking 有后台支持吗? [英] Does AFNetworking have backgrounding support?
问题描述
我正在研究 AFNetworking
作为 ASIHTTPRequest
的替代品,并注意到完全缺乏关于它是否支持后台下载/上传的信息.>
使用 ASIHTTPReqeust
对象,您所要做的就是调用 [request setShouldContinueWhenAppEntersBackground:YES]
并且请求将在后台继续.AFNetworking
是否对此有任何支持?
EDIT:截至 AFNetworking 1.0RC1,这是一个显式功能.AFURLConnectionOperation
现在拥有方法 setShouldExecuteAsBackgroundTaskWithExpirationHandler:
,它为您透明地管理所有这些.
这是一个隐含的功能,所以我并没有真正考虑宣传它.您需要做的就是:
<前><代码>- (void)applicationWillResignActive:(UIApplication *)application {__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {[应用结束BackgroundTask:backgroundTaskIdentifier];[[YourRestClient sharedClient] cancelAllHTTPOperations];}];}或者,如果您在自己的 NSOperationQueue
中管理操作,只需在此处 -cancelAllOperations
代替.
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
?
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.
这篇关于AFNetworking 有后台支持吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!