在后台继续执行某些操作的一种方法是创建一个单独的线程来进行下载。在线程内部,将您的下载操作括在对 beginBackgroundTaskWithExpirationHandler: 和 endBackgroundTask 的调用之间。您不需要检查您是否在后台运行,您只需始终调用这两个方法。
// 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];