AFNetworking 有后台支持吗? [英] Does AFNetworking have backgrounding support?

查看:30
本文介绍了AFNetworking 有后台支持吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆