如果应用程序在NSURLConnection异步请求返回之前休眠或死亡怎么办?

时间:2011-09-02 16:14:18

标签: nsurlconnection nsurlconnectiondelegate

当NSURLConnection异步请求返回时,如果应用程序死机,死亡,睡着或正在退出,会发生什么?

在我看来,在我的异步函数假定程序数据有效之前,我最好保留一个标记“程序正在退出”。

感谢。

1 个答案:

答案 0 :(得分:0)

在iOS 4.0及更高版本中,当用户关闭应用时,它会进入暂停状态。除非您告诉它不这样做,否则在用户再次打开应用程序之前,代理回调不会触发。您可以通过在回调中编写一些NSLog来测试自己。然后,在测试设备上运行应用程序,并在看到连接开始后尝试关闭应用程序。稍等片刻,重新打开应用程序(仍然在Xcode中“运行”),并在调用委托回调时观察。

//somewhere just after you start the async connection
NSLog(@"Connection started..."); 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed with error - %@",error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"D- Connection finished loading");
} 

如果您希望在用户关闭应用程序后继续运行连接,请在“iOs应用程序编程指南”中阅读“Executing Code in the Background”。