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

发布于 2024-12-02 11:00:48 字数 131 浏览 1 评论 0 原文

当 NSURLConnection 异步请求返回时,如果应用程序已死亡、即将死亡、睡眠或正在退出,会发生什么情况?

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

谢谢。

What will happen if an app is either dead, dying, asleep, or in the process of exiting when an NSURLConnection async request comes back?

It seems to me I'd better at a minimum keep a flag that says "program is exiting" before my async function presumes that program data is valid.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2024-12-09 11:00:48

在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");
} 

如果您希望连接在用户关闭应用程序后继续运行,请通读 '在后台执行代码'。

In iOS 4.0 and above, when the user closes the app, it is put into a suspended state. Unless you tell it to do otherwise, the delegate callbacks won't fire until the user opens the app again. You can test yourself by writing some NSLogs in your callbacks. Then, run the app on a test device, and try closing the app just after you see the connection start. Wait a while and reopen the app (still 'running' in Xcode), and watch as the delegate callbacks get called.

//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");
} 

If you'd like the connection(s) to continue to run after the app is closed by the user, then have a read through 'Executing Code in the Background' in the iOs Application Programming Guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文