Search code examples
iphoneiosnsurlconnectionnsoperationnsoperationqueue

setDelegateQueue is not working on iOS5 but working fine on iOS6


I am trying to perform a downloading in a background thread.For this, I have created an instance of NSOperationQueue and add an instance of NSInvocationOperation to it.

        operationQueue = [NSOperationQueue new];
        // set maximum operations possible
        [operationQueue setMaxConcurrentOperationCount:1];

        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                                selector:@selector(startBackgroundThread:)
                                                                                  object:data];

        [operationQueue addOperation:operation];
        [operation release];

Now in startBackgroundThread,I have created a new NSURLConnection and dispatching the operationQueue to it.

    currentConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    if (self.operationQueue) {
        [currentConnection setDelegateQueue:self.operationQueue];
    }        
    [currentConnection start];

Behavior in iOS6

Working exactly fine on iOS6. A thread gets created and NSURLConnection callbacks it delegates on the same background thread.

Behavior in iOS5 and iOS5.1

A thread gets created but NSURLConnection delegates didn't get called.

Am i missing something that need to be taken in account in iOS5? I have read the documention provided by Apple but nothing is mentioned there related to that.


Solution

  • If you are trying to perform a background web request then use the following convenience method.

    [NSURLConnection sendAsynchronousRequest: queue: completionHandler:];
    

    Block apis give better performance and recommended by apple.