I got confused while working with NSURLConnection and NSRunLoop. I’m trying to download a large file using NSURLConnection but it’s NOT working (Not even calling a single delegate method) as expected.
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[request setHTTPBody:[@"Request Body Data" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
Running on main Thread.
Only working for small size of file. It didn’t work when I try to download an 18MB file.
It didn't work for large image files.
[NSURLConnection connectionWithRequest:request delegate:self];
It didn't work for large image file, tried with 18MB.
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; [con setDelegateQueue:[NSOperationQueue currentQueue]]; [con start];
It didn't work for large image file, tried with 18MB.
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; [con scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; // Or NSRunLoopCommonModes [con start];
It didn't work for large image file, tried with 18MB.
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; NSRunLoop *loop = [NSRunLoop currentRunLoop]; [con scheduleInRunLoop:loop forMode:NSRunLoopCommonModes]; [con start]; [loop run];
It didn't work for large image file, tried with 18MB.
NSHTTPURLResponse *res = nil; NSError *err = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&res error:&err]; NSLog(@"Res Code: %d, DataLen: %d", res.statusCode, data.length);
It WORKED for large image as well
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSHTTPURLResponse *res = (NSHTTPURLResponse*)response; NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/myImage.jpg"]; NSLog(@"Res Code: %d, DataLen: %d, Path: %@", res.statusCode, data.length, imagePath); [data writeToFile:imagePath atomically:YES]; }];
Running on NSOperation (I have attempted same above code in NSOperation's subclass, I have uploaded the code on github. Please find the link below.)
- Nothing worked (None of the delegate method get called.)
- It WORKED for all.
- It didn’t work for large file.
- It WORKED for all. (It worked for NSDefaultRunLoopMode mode as well, where apple document says this “The mode to deal with input sources other than NSConnection objects.”).
- It WORKED for all.
- It WORKED for all.
I just want to understand the basic logic behind NSRunLoop, when we use it with NSURLConnection object.
How NSRunLoop works behind the scene? How does it work with NSURLConnections?
What happens when we call any asynchronous request through NSURLConnection on main thread or any secondary thread (created by NSOperation)?
I have read several blogs and apple documents related to NSRunLoop but still confused, so i have written a doc over my understanding about the same. NSRunLoop understanding Doc.
Many thanks in advance!