Background Network Calls - iOS

I need to send some data to a web server in the background. To just clarify โ€œin the background,โ€ I don't mean the usual way of displaying a rotating icon and placing data in a web service using something like the AsyncTask or ASIHTTPRequest [request startAsynchronous] . I need to maintain a data queue that Thread can asynchronously launch and send to a web service while the user is working in the application.

I am looking for some help in developing such a queue, especially in some cases with edges, such as the user receiving the call, exiting the application while recording, the user leaves the application to go to another while the message happens and the like. How would you handle these cases? Is there any source code you can recommend for this?

Thanks,
Thea

+6
source share
4 answers

I started using NSOperationQueue in my own work recently to manage background network requests. NSOperation deals with most of the template code needed for asynchronously executing tasks (such as network operations) in threads in the background (or foreground, if necessary for user interface updates).

It also allows you to set dependencies between queues; for example, I use two queues in my application:

The first graph is loading images with a maximum concurrency of 2 at a time, in the background. Each image download has a corresponding completion handler (like NSBlockOperation ), which depends on the completion of the image loading. These operations are hosted on [NSOperationQueue mainQueue] , which runs in the main thread, which allows them to update the interface (in particular, the corresponding UIImageView ).

Please note that NSOperation and NSOperationQueue not only for network queries, but also for any operation that can be divided into atomic tasks and scheduled at the same time.

Here are Apple's intro docs on this topic.

+3
source

Having implemented something similar, I would recommend using a service rather than a stream for network calls. Thus, even if your activity is killed, you are sure that your network calls will be completed.

Then, to implement the queue, I suggest you take a look at IntentService (http://developer.android.com/reference/android/app/IntentService.html)

from documents:

IntentService is the base class for services that process asynchronous requests (expressed as intentions) on demand. Clients send requests through startService (Intent) calls; the service starts as needed, processes each intention, in turn, using the workflow and stops when it ends.

This work queue pattern is typically used to offload tasks from the main application thread. The IntentService class exists to simplify this scheme and take care of the mechanics. To use it, extend the IntentService and implement onHandleIntent (Intent). IntentService will receive Intents, start a workflow, and stop servicing as needed.

All requests are processed on a single workflow - they can be accepted as needed (and will not block the main application loop), but only one request will be processed at a time.

If your application is simple enough, you can use sendBroadCast () to exchange information and notifications between your activity and IntentService

+2
source

Create a singleton that encapsulates the stream:

In the initialization of your object:

 [NSThread detachNewThreadSelector:@selector(mainPosterThread) toTarget:self withObject:nil]; - (void)mainDownloaderThread { if( [NSThread respondsToSelector:@selector(setThreadPriority:)] ) { [NSThread setThreadPriority:0.1]; } NSString *urlToDownload = nil; while(shouldRun) { // Take next data to post in a queue (lastObject of a NSArray for example) if( nextDataToPost ) { // Post } else { // Sleep for some time. } } } 

You may also have methods for stopping / starting a thread while the application goes into background / foreground on a multitasking device. If multitasking support is not supported, save the mail data in a queue during a stop (if not too long) and restore it at startup. The biggest hurdle is managing the ability to cancel the current download while the application is finished.

0
source

This is a problem that I am improving in every new application that I write. Basically, I wanted the network functions to be asynchronous and which I wrote using the built-in functions. I would be happy to show you some of this code if you are interested.

First of all, I suggest that you make all network calls in the main thread asynchronously , not synchronously using a delegate. So serialization / sync / concurrency is not a problem. And since the classes are network delegates, I just sent one class to where the new connection has a new delegate instance.

 [[NSURLConnection alloc] initWithRequest:request delegate:del] autorelease]; eg - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
0
source

Source: https://habr.com/ru/post/893791/


All Articles