继续在后台下载 [英] Continue download in background

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

问题描述

我正在创建一个应用程序,其中我正在从服务器下载一些数据.在后台运行时,我希望该连接应继续运行,以便可以下载数据.我知道 appDelegate

I am creating an application wherein I am downloading some data from server. While going in background I want that connection should continue running so that data can be downloaded. I know there is method in appDelegate

- (void)applicationDidEnterBackground:(UIApplication *)application  

,当应用程序进入后台时被调用.但是,由于连接是在viewController中创建的,因此如何在 appDelegate 中进行管理?
另外,还有其他方法可以做到吗?我已经通过> 链接,但是有一些问题简单实施?

which is called when application enters background. But as the connection is created in viewController, how can it be managed in appDelegate?
Also is/are there other way(s) this can be done? I have gone through this link but is there a something simple to implement?

推荐答案

一种在后台继续进行的操作的方法是创建一个单独的线程来进行下载.在线程内,将您的下载操作括在对beginBackgroundTaskWithExpirationHandler:和endBackgroundTask的调用之间.您无需检查是否在后台运行,只需调用这两个方法即可.

One way to do some operations that continue in the background is to create a separate thread to do the downloading. Inside the thread, bracket your download operations between calls to beginBackgroundTaskWithExpirationHandler: and endBackgroundTask. You don't need to check to see whether you are running in the background or not, you just always call these two methods.

// Tell iOS this as a background task in case we get backgrounded
UIBackgroundTaskIdentifier taskId = [[UIApplication sharedApplication] 
          beginBackgroundTaskWithExpirationHandler:NULL];

//----------------------------------------------
// Perform your download operations here
//----------------------------------------------
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

// Tell iOS that we are done with stuff that needed to keep going even if backgrounded    
[[UIApplication sharedApplication] endBackgroundTask:taskId];

这篇关于继续在后台下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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