简体   繁体   English

在应用程序进入后台时取消API请求

[英]canceling API request while application entering into background

While launching the application in my iPhone, I need to download the data from the server. 在iPhone上启动应用程序时,我需要从服务器下载数据。 While downloading data(in middle of process) if I press home button to enter the application into background.this time I need to stop the API request which is already executing. 在下载数据时(过程中),如果我按主页按钮将应用程序进入后台。这次,我需要停止已经执行的API请求。 currently applicationDidEnterBackground method is calling with delay(after downloading data). 当前applicationDidEnterBackground方法正在延迟调用(下载数据后)。 In the mean time application is crashing. 同时,应用程序崩溃了。 how can we cancel the URL connection while application is entering to background. 当应用程序进入后台时,如何取消URL连接。

Please help me out. 请帮帮我。

Thanks in advance. 提前致谢。

One way to do it is to set some kind of flag in your app controller that you can periodically check to see if the request is cancelled. 一种方法是在应用程序控制器中设置某种标记,您可以定期检查该标记以查看请求是否已取消。

Or, even better, the NSThread object provides a mechanism to indicate that it should be cancelled. 或者,甚至更好的是, NSThread对象提供了一种指示应取消它的机制。 If you go into the background, set your detached thread to cancel (via [NSThread cancel] ) and then in whatever API callsback you have, you should periodically check to see if [NSThread isCancelled] is returning YES . 如果进入后台,请将分离的线程设置为cancel(通过[NSThread cancel] ),然后在具有任何API回调的情况下,都应定期检查[NSThread isCancelled]是否返回YES

Here's a link to Apple's documentation on [NSThread cancel] . 这是指向[NSThread cancel]上Apple文档的链接。

http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html#//apple_ref/occ/instm/NSThread/cancel http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html#//apple_ref/occ/instm/NSThread/cancel

hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM