Asynchronous NSURLConnection with NSOperation
Asked Answered
N

3

14

I want to do NSURLConnection in background mode,because it response is having much data.Forums are telling to use Apple's finite length coding to use in didEnterBackground. but I want to avoid it.instead of it I use following code through NSOperation with NSInvocation as, but it is not working.connectToServer is having NSURLConnection operation.any help please?didReceiveData,didReceiveResponse delegate methods are not called?

 NSOperationQueue *queue = [NSOperationQueue new];

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

[queue addOperation:operation];
[operation release];
[queue autorelease];

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

    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
}
Nickolasnickolaus answered 10/2, 2012 at 6:14 Comment(0)
T
42

This kind of question was asked a zillion time. Delegates are not getting called because as of iOS 4 operations are started on a secondary thread. The thread probably exits before the delegates are called that's all.

Keep the connection on the main thread and handle the data in a background thread using GCD.

I'v wrote about all this stuff here : http://cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/

EDIT : Updated link.

Touchback answered 13/2, 2012 at 13:51 Comment(5)
enjoy my hard earned 50 votes.u should have answered before my bounty :)Nickolasnickolaus
can you please tell how to stop Runloop and port?Nickolasnickolaus
[runLoop removePort:PORT forMode:MODE]; CFRunLoopStop(CFRunLoopGetCurrent());Touchback
Thanks a lot @Nyx0uf, you save me a lot of time...! Great contribution!Lied
I don't think that this is a solution, the answer is a workaround, but not the solution to the problem.Cysticercus
A
6

Apple provides QHTTPOperation, which does just what you want, encapsulates an NSURLConnection within an NSOperation, for a single request. You can find it in Apple's sample code.

QHTTPOperation is actually a subclass of QRunLoopOperation which lets you encapsulate logic which depends on run-loop callbacks in an NSOperation.

The 3rd party ASIHTTPRequest is a similar popular solution, AFAIK it is no longer maintained though.

Accad answered 19/2, 2012 at 21:19 Comment(0)
L
0

Look at the sendAsynchronousRequest:queue:completionHandler: method documented here in Apple's Documentation. It allows asynchronous requests with NSURLConnection.

Note this requires iOS5 or higher

Lankford answered 10/2, 2012 at 6:34 Comment(2)
The first link I had was for the Mac, I have updated it for the iOS version. Are you wanting the Mac reference? The Mac documentation can be found here: developer.apple.com/library/mac/documentation/Cocoa/Reference/…Lankford
i want to do in iOS4..please answer to my question why it is not working?Nickolasnickolaus

© 2022 - 2024 — McMap. All rights reserved.