Search code examples
iphoneobjective-cconnectionnsoperationnsoperationqueue

How do I start an Asynchronous NSURLConnection inside an NSOperation?


I want to do an Asynchrous NSURLConnection inside of an NSOperation on a background thread. it is because I'm doing some very expensive operations on the data as they come back.

This is a very similar question to what they asked here: How do I do an Asynchronous NSURLConnection inside an NSOperation?

but the difference is that I run the connection in another class.

Here is my first attempt:

In my MainViewController:

@property (nonatomic, strong) NSOperationQueue *requestQueue;

#pragma mark - Lazy initialization
- (NSOperationQueue *)requestQueue 
{
    if (!_requestQueue) {
        _requestQueue = [[NSOperationQueue alloc] init];
        _requestQueue.name = @"Request Start Application Queue";
        _requestQueue.maxConcurrentOperationCount = 1;
    }
    return _requestQueue;
}

-(void)callToServer
{
URLJsonRequest *request = [URLRequestFactory createRequest:REQUEST_INTERFACE_CLIENT_VERSION
                                                         delegate:self];

    RequestSender *requestSender = [[RequestSender alloc]initWithPhotoRecord:request delegate:self];

   [self.requestQueue addOperation:requestSender];
}

Here is my operation:

- (id)initWithPhotoRecord:(URLJsonRequest *)request
                 delegate:(id<RequestSenderDelegate>) theDelegate{

    if (self = [super init])
    {
        self.delegate = theDelegate;
        self.jsonRequest = request;
    }
    return self;
}

- (void)main {

    //Apple recommends using @autoreleasepool block instead of alloc and init NSAutoreleasePool, because blocks are more efficient. You might use NSAuoreleasePool instead and that would be fine.
    @autoreleasepool
    {

        if (self.isCancelled)
            return;

        [self.jsonRequest start];

    }
}

Here is my Request start function:

-(void) start
{
  NSURL *url = [NSURL URLWithString:@"http://google.com"];
 NSURLRequest *theRequest = [NSURLRequest requestWithURL:url];
  urlConnection = [[[NSURLConnection alloc]    initWithRequest:theRequest delegate:self]autorelease];

[urlConnection start];
[theRequest release]
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"Received reponse from connection");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{



}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

}

I do not get a response from the server.


Solution

  • A couple of approaches:

    1. Schedule the NSURLConnection in the main run loop, by using the startImmediately parameter of NO, set the run loop, and only then should you start the connection, e.g.:

      urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:NO];
      [urlConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
      [urlConnection start];
      
    2. Create a dedicated thread for the the connection and schedule the connection in the run loop you create for that thread. See AFURLConnectionOperation.m in AFNetworking source for an example of this.

    3. Actually use AFNetworking, which gives you NSOperation based operations that you can add to your queue, and takes care of this run loop stuff for you.


    So, AFNetworking does something like:

    + (void)networkRequestThreadEntryPoint:(id)__unused object {
        @autoreleasepool {
            [[NSThread currentThread] setName:@"NetworkingThread"];
    
            NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
            [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
            [runLoop run];
        }
    }
    
    + (NSThread *)networkRequestThread {
        static NSThread *_networkRequestThread = nil;
        static dispatch_once_t oncePredicate;
    
        dispatch_once(&oncePredicate, ^{
            _networkRequestThread = [[NSThread alloc] initWithTarget:self
                                                            selector:@selector(networkRequestThreadEntryPoint:)
                                                              object:nil];
            [_networkRequestThread start];
        });
    
        return _networkRequestThread;
    }
    

    So I do something like the following. First I have a few private properties:

    @property (nonatomic, readwrite, getter = isExecuting)  BOOL executing;
    @property (nonatomic, readwrite, getter = isFinished)   BOOL finished;
    @property (nonatomic, weak)   NSURLConnection *connection;
    

    Then the network operation can then do something like:

    @synthesize executing = _executing;
    @synthesize finished  = _finished;
    
    - (instancetype)init {
        self = [super init];
        if (self) {
            _executing = NO;
            _finished = NO;
        }
        return self;
    }
    
    - (void)start {
        if (self.isCancelled) {
            [self completeOperation];
            return;
        }
    
        self.executing = YES;
    
        [self performSelector:@selector(startInNetworkRequestThread)
                     onThread:[[self class] networkRequestThread]
                   withObject:nil
                waitUntilDone:NO];
    }
    
    - (void)startInNetworkRequestThread {
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:self.request
                                                                      delegate:self
                                                              startImmediately:NO];
        [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
        [connection start];
    
        self.connection = connection;
    }
    
    - (void)completeOperation {
        self.executing = NO;
        self.finished = YES;
    }
    
    - (void)setFinished:(BOOL)finished {
        if (finished != _finished) {
            [self willChangeValueForKey:@"isFinished"];
            _finished = finished;
            [self didChangeValueForKey:@"isFinished"];
        }
    }
    
    - (void)setExecuting:(BOOL)executing {
        if (executing != _executing) {
            [self willChangeValueForKey:@"isExecuting"];
            _executing = executing;
            [self didChangeValueForKey:@"isExecuting"];
        }
    }
    
    - (BOOL)isConcurrent {
        return YES;
    }
    
    - (BOOL)isAsynchronous {
        return YES;
    }
    
    // all of my NSURLConnectionDataDelegate stuff here, for example, upon completion:
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        // I call the appropriate completion blocks here, do cleanup, etc. and then, when done:
    
        [self completeOperation];
    }