2011-06-21 92 views
0

我有一个使用ASIHTTP请求运行的上传队列。当用户按主屏幕并且应用程序进入后台时,我希望继续此操作。从文档中我可以看到我将如何调用一个可以在后台运行的新任务,但无法完全了解如何将正在运行的任务标记为继续。在iOS4的后台继续长时间运行的进程

回答

2

这个例子中括号内为4.0版本之前的兼容性:

UIApplication *app = [UIApplication sharedApplication]; 
if ([app respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) { 
    backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (backgroundTaskIdentifier != UIBackgroundTaskInvalid) 
      { 
       // you took too long - clean up what you can, then … 
       [app endBackgroundTask:backgroundTaskIdentifier]; 
       backgroundTaskIdentifier = UIBackgroundTaskInvalid; 
      } 
     }); 
    }]; 
} 

// start HTTP request … 

当你完成你的过程中,你应该叫endBackgroundTask:让应用程序知道

+0

也可以尝试加入 UIBackgroundTaskIdentifier __block backgroundTaskIdentifier; 此块之前。 :) – huesforalice

相关问题