Wednesday, May 1, 2013

NSURLConnection setDelegateQueue example ios


setDelegateQueue:

Determines the operation queue that is used to send messages to the connection’s delegate.
- (void)setDelegateQueue:(NSOperationQueue *)queue
Parameters of [NSURLConnection setDelegateQueue]
queue
The operation queue to use for delegate messages.
Discussion of [NSURLConnection setDelegateQueue]
By default, a connection is scheduled on the current thread in the default mode when it is created. If you create a connection with the initWithRequest:delegate:startImmediately: method and provide NO for the startImmediately parameter, you can instead schedule the connection on an operation queue before starting it with the start method.
You cannot reschedule a connection after it has started.
It is an error to schedule delegate messages with both this method and thescheduleInRunLoop:forMode: method.
Example of [NSURLConnection setDelegateQueue]
[queue addOperationWithBlock:^{
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                                  delegate:self
                                                          startImmediately:NO];
    [connection setDelegateQueue:queue];
    [connection start];
}];
Example of [NSURLConnection setDelegateQueue]
currentConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
if (self.operationQueue) {
    [currentConnection setDelegateQueue:self.operationQueue];
}        
[currentConnection start];