Background processing in iOS;

Posted on

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 incase I’m not doing the thing in 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 user’s device goes in sleep mode or user goes away from my applition (either via pressing home button, or by selecting some other app from minimize 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 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 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 scenario? Moreover when our app enters in background then what happen 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)

What is the significance of beginBackgroundTaskWithExpirationHandler and UIBackgroundTask? 

Another thing is (specially for my scenario) if we start a download process then what happen to this process at time when app enters in background?  Does iOS allow to continue downloading the data when app enters in background? Or it just pause it? Right now, I’m assuming that my download get paused and then resume when app will enter in 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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s