Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Async/Background POST without using delegate method?

I need to make some POST calls to my server, but I need to not block the main thread. As I understand, NSMutableURLRequest and NSURLConnection are not thread safe, so it is best to use the async method of NSURLConnection.

My question about this is, how I can package it up nicely into a method, instead of having to use the delegate method? I would prefer to do:

NSData *returnedData = [Utility postDataToURL:@"some string of data"];

This is how it is easy done with the following method:

[NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];

It is so nice keeping everything within one method, then just having my data returned from it!

Are there any block based methods for this? It becomes an issue when I need to write methods for about 50 different calls and each one needs to use the same delegate method. Am I going about this the wrong way?

This will only need to be for iOS5.

like image 456
Nic Hubbard Avatar asked Feb 23 '12 00:02

Nic Hubbard


2 Answers

iOS 5 adds sendAsynchronousRequest:queue:completionHandler: which does what I think you want. I've got my code set up to use that if available, but to fall back on performing a synchronous fetch on a background GCD queue and hopping onto the main thread with the result if it doesn't. The latter will be less power efficient but it's just to maintain legacy support.

if([NSURLConnection respondsToSelector:@selector(sendAsynchronousRequest:queue:completionHandler:)])
{
    // we can use the iOS 5 path, so issue the asynchronous request
    // and then just do whatever we want to do
    [NSURLConnection sendAsynchronousRequest:request
        queue:[NSOperationQueue mainQueue]
        completionHandler:
        ^(NSURLResponse *response, NSData *data, NSError *error)
        {
            [self didLoadData:data];
        }];
}
else
{
    // fine, we'll have to do a power inefficient iOS 4 implementation;
    // hop onto the global dispatch queue...
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
    ^{
        // ... perform a blocking, synchronous URL retrieval ...
        NSError *error = nil;
        NSURLResponse *urlResponse = nil;
        NSData *responseData =
            [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

        // ... and hop back onto the main queue to handle the result
        dispatch_async(dispatch_get_main_queue(),
        ^{
            [self didLoadData:responseData];
        });
    });
}

In production code you'd actually check the errors and HTTP response codes (as a server 404 response is probably just as much an error from your point of view as a connection failure), obviously.

like image 149
Tommy Avatar answered Oct 19 '22 14:10

Tommy


iOS 5.0 > you can use sendAsynchronousRequest method look at NSURLConnection Class and it uses blocks. If you want to support iOS 4.0 > too then you have to write one of your own block based Asynchronous URL loading which is fairly easy to write. You are better off by using MKNetworkKit.

but I need to not block the main thread. As I understand, NSMutableURLRequest and NSURLConnection are not thread safe, so it is best to use the async method of NSURLConnection.

You don't want to do Synchronous network connection it blocks thread whichever it is called from (its even worse if its main thread). You can do Asynchronous network connection on main thread. If you want to do call NSURLConnection on non-main thread then have to create a RunLoop on that thread (if you don't then the delegate methods of NSURLConnection never gets called).

like image 41
0x8badf00d Avatar answered Oct 19 '22 15:10

0x8badf00d