While launching the application in my iPhone, I need to download the data from the server. 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. currently applicationDidEnterBackground method is calling with delay(after downloading data). In the mean time application is crashing. how can we cancel the URL connection while application is entering to background.
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
NSThreadobject provides a mechanism to indicate that it should be cancelled. 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 returningYES.Here’s a link to Apple’s documentation on
[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!