Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background network calls - iOS

I need to implement posting some data to a web server in the background. Just to clarify, by "in the background", I don't mean the normal way of showing a spinning icon and posting data to a web service using something like an AsyncTask or ASIHTTPRequest's [request startAsynchronous] method. I need to maintain a queue of data that a Thread can asychronously start processing and posting to a Web service while the user is working in the application.

I'm looking for some help on designing a queue like that, especially in some edge cases like User receiving a call, logging out of the application while the the post is happening, user leaving the application to goto a different one while a post is happening and the like. How would you handle these cases? Is there any source code you can recommend that does this?

Thanks,
Teja.

like image 837
Tejaswi Yerukalapudi Avatar asked Jul 28 '11 15:07

Tejaswi Yerukalapudi


People also ask

What is background fetch iOS?

See: https://developer.apple.com/reference/uikit/uiapplication#1657399 iOS Background Fetch is basically an API which wakes up your app about every 15 minutes (during the user's prime-time hours) and provides your app exactly 30s of background running-time.

Can iOS apps run in the background?

If an app plays audio in the background (over AirPlay or through the phone's speakers), iOS permits it to run in the background until it ceases to play the music; if an app allows you to make data-based phone calls (like Whatsapp or Skype calls) in the background, it can stay active, using CPU for the duration of the ...

What is background mode?

During background monitoring, the video and audio are not transmitted, which helps to save your battery on the Person Station device. You'll also be able to use other audio apps (e.g., music and video players). To use the background mode, make sure to enable notifications on your Person Station's device.


1 Answers

I've started using NSOperationQueue in my own work lately, for controlling background network requests. NSOperation deals with most of the boilerplate code necessary for asynchronously running tasks (such as network operations) on threads in the background (or foreground, if necessary for UI updates).

It also allows dependencies across queues; for example, I use two queues in my application:

The first schedules image downloads, at a max concurrency of 2 at a time, in the background. Each image download has a corresponding completion handler (as an NSBlockOperation) that is dependent on the image download completing. These operations sit on the [NSOperationQueue mainQueue], which operates on the main thread, allowing them to update UI (specifically, the corresponding UIImageView).

Note that NSOperation and NSOperationQueue are not for network requests only, but any operation that can be divided into atomic tasks and scheduled concurrently.

Here are Apple's intro docs on the topic.

like image 144
Ben Mosher Avatar answered Oct 22 '22 07:10

Ben Mosher