{"id":3296,"date":"2012-06-07T13:10:15","date_gmt":"2012-06-07T08:10:15","guid":{"rendered":"http:\/\/aasims.wordpress.com\/?p=3296"},"modified":"2025-04-27T18:25:40","modified_gmt":"2025-04-27T18:25:40","slug":"background-processing-in-ios","status":"publish","type":"post","link":"https:\/\/aasimnaseem.com\/blog\/background-processing-in-ios\/","title":{"rendered":"Background processing in iOS"},"content":{"rendered":"<p>Hello, everyone.<br \/>\nHope things are going well around you.<\/p>\n<p>I&#8217;m working on an iOS application and facing an issue regarding network calls. I need your kind suggestions in incase I&#8217;m not doing the thing in the right way.<\/p>\n<p>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&#8217;m using ASIHTTPRequest\u00a0at my network layer.<\/p>\n<p>Issue: I found that during download, if the user&#8217;s device goes into sleep mode or the user goes away from my application (either via pressing the home button or by selecting some other app from the minimize bar or via multi-gesture with four fingers to swap to the next application), the downloading process terminates by throwing either a timeout or a connection break. Hence, as a result, the downloading fails.<\/p>\n<p>I was using the following piece of code.<\/p>\n<p>[code lang=&#8221;objc&#8221;]<br \/>\nself.request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];<br \/>\n[self.request setRequestMethod:@&#8221;GET&#8221;];<br \/>\n[self.request setDownloadDestinationPath:paramFilePath];<br \/>\n[self.request startSynchronous];<br \/>\n[\/code]<\/p>\n<p>Adopted Solution:<\/p>\n<p>Later on, after reading some blog posts, I added the following attribute to my request object:<\/p>\n<p>[code lang=&#8221;objc&#8221;]<br \/>\n[self.request setShouldContinueWhenAppEntersBackground:YES];<br \/>\n[\/code]<\/p>\n<p>But the problem was still there.<br \/>\nAfter spending some more time on StackOverflow, I started using the following attribute in my request:<\/p>\n<p>[code lang=&#8221;objc&#8221;]<br \/>\n[request setTemporaryFileDownloadPath:tempFilePath];<br \/>\n[self.request setAllowResumeForFileDownloads:YES];<br \/>\n[\/code]<\/p>\n<p>Somehow my app is now behaving well. If I pick 4 or 5 items and start downloading, it doesn&#8217;t interrupt the download process even if I navigate away from my app.<\/p>\n<p>Questions: So the questions are, am I doing it right? Or are there some other better ways to handle such a scenario? Moreover, when our app enters the background, then 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 handle it (due to shouldContinueWhenAppEntersBackground attribute)<\/p>\n<p>What is the significance of\u00a0beginBackgroundTaskWithExpirationHandler\u00a0and\u00a0UIBackgroundTask?<\/p>\n<p>Another thing is (especially for my scenario), if we start a download process, then what happens to this process at the time when the app enters the background? Does iOS allow you to continue downloading the data when the app enters the background? Or just pause it?\u00a0 Right now, I&#8217;m assuming that my download will get paused and then resume when the app enters the foreground again. Is that right? Plus, is it confirmed that we can&#8217;t continue download progress from\u00a0foreground-\u2063background-\u2063foreground? We have to bear a pause and resume cycle. Right?<\/p>\n<p>Kindly reply in the comments about your experiences with such scenarios.<\/p>\n<p>Thanks in advance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, everyone. Hope things are going well around you. I&#8217;m working on an iOS application and facing an issue regarding network calls. I need your kind suggestions in incase I&#8217;m not doing the thing in the right way. Scenario: I want to download one or more zip files from some&#8230;<\/p>\n","protected":false},"author":1,"featured_media":4636,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[162,196,345,544,917,1035],"class_list":["post-3296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","tag-asihttprequest","tag-beginbackgroundtaskwithexpirationhandler","tag-downloading-in-background","tag-ios","tag-setshouldcontinuewhenappentersbackground","tag-uibackgroundtask"],"_links":{"self":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/3296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/comments?post=3296"}],"version-history":[{"count":3,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/3296\/revisions"}],"predecessor-version":[{"id":4637,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/3296\/revisions\/4637"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media\/4636"}],"wp:attachment":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media?parent=3296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/categories?post=3296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/tags?post=3296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}