Background processing in iOS;

Aasim Naseem 0
Apple iOS

Hello everyone;
Hope things are going good around you;

I’m working on an iOS application and facing an issue regarding network calls; Need your kind suggestions in case I’m not doing the thing in the right way;

Scenario: I want to download one or more zip files from some URLs; The files may have different sizes, ranges from 6MB to 150MB; I’m using ASIHTTPRequest at my network layer;

Issue: I found that during download if the user’s device goes in sleep mode or the user goes away from my application (either via pressing the home button, or by selecting some other app from minimizing bar, or via multi-guester with four fingers to swap to next application) the downloading process terminates by throwing either time out or connection break; Hence as a result the downloading fails;

I was using the following piece of code;

self.request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
[self.request setRequestMethod:@”GET”];
[self.request setDownloadDestinationPath:paramFilePath];
[self.request startSynchronous];

Adopted Solution:

Later on, after reading some blog posts, I added the following attribute in my request object;

[self.request setShouldContinueWhenAppEntersBackground:YES];

But the problem was still there;
After spending some more time over StackOverflow, I started using the following attribute in my request;

[request setTemporaryFileDownloadPath:tempFilePath];
[self.request setAllowResumeForFileDownloads:YES];

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 there are some other better ways to handle such scenarios? Moreover when our app enters in the background then what happens with our ASI calls (sync/async calls)? Does iOS stop/pause them? or let them continue running in the background so that when the response arrives our app 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 a time when the app enters in the background?  Does iOS allow to continue downloading the data when the app enters in the background? Or it just pauses it? Right now, I’m assuming that my download gets paused and then resume when the app will enter in the foreground again; Is that right? Plus is it confirm that we can’t continue download progress from foreground -> background ->foreground? We have to bear a pause and resume cycle; Right?

Kindly reply in comments about your experiences with such scenarios;

Thanks in advance;

free counters

Leave a Reply

Your email address will not be published.