Background processing in iOS; Aasim Naseem, June 7, 2012April 27, 2024 Hello everyone; I hope things are going well around you.I’m working on an iOS application and facing an issue regarding network calls. I need your kind suggestions incase I’m not doing things the right way.Scenario: I want to download one or more zip files from some URLs. The files may have different sizes, ranging from 6MB to 150MB. I’m using ASIHTTPRequest at my network layer.Issue: I found that during download, if user’s device goes in sleep mode or user goes away from my application (either via pressing home button, selecting some other app from minimize bar, or via multi-guest with four fingers to swap to next application), the downloading process terminates by throwing either a timeout or connection break. Hence, as a result, the downloading fails;I was using following piece of code;[code lang=”objc”] self.request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]]; [self.request setRequestMethod:@”GET”]; [self.request setDownloadDestinationPath:paramFilePath]; [self.request startSynchronous]; [/code]Adopted Solution:Later on, after reading some blog posts, I added the following attribute in my request object;[code lang=”objc”] [self.request setShouldContinueWhenAppEntersBackground:YES]; [/code]But the problem was still there; After spending some more time over stackoverflow, I started using following attribute in my request;[code lang=”objc”] [request setTemporaryFileDownloadPath:tempFilePath]; [self.request setAllowResumeForFileDownloads:YES]; [/code]Somehow my app is now behaving well; If I pick 4, 5 items and start downloading, it doesn’t interrupt the download process even if I navigate away from my app;Questions: So the questions are: am I doing it right? Or are there some other better ways to handle such scenario? Moreover, when our app enters the background, what happens with our ASI calls (sync/async calls)? Does iOS stop/pause them? or let them continue running in background so that when response arrives, our app will handle it (due to shouldContinueWhenAppEntersBackground attribute)What is the significance of beginBackgroundTaskWithExpirationHandler and UIBackgroundTask? Another thing is (especially for my scenario) if we start a download process, then what happens to this process at time when app enters the background? Does iOS allow to continue downloading the data when app enters the background? Or it just pause? Right now, I’m assuming that my download get paused and then resumes when app enters the foreground again; Is that right? Plus, does it confirm that we can’t continue download progress from foreground to background to foreground? We have to take a pause and resume cycle; Right?Kindly reply in comments about your experiences with such scenarios;Thanks in advance;Aasim NaseemI’m an engineer by profession, a blogger and a photojournalist by hobby. Seasonal writer at LAFZ Media UK. I write on different topics and things around us. Contact me at +971.56.126.8842 or Aasim.Naseem@outlook.com Tips & Tutorials iOS ASIHTTPRequestbeginBackgroundTaskWithExpirationHandlerdownloading in backgroundiossetShouldContinueWhenAppEntersBackgroundUIBackgroundTask