开发者

What if app sleeping or dead before NSURLConnection asynchronous request comes back?

开发者 https://www.devze.com 2023-04-02 16:16 出处:网络
What will happen if an app is either dead, dying, asleep, or in the process of exiting when an NSURLConnection async request comes back?

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 i开发者_StackOverflow社区s exiting" before my async function presumes that program data is valid.

Thanks.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消