NSURLConnection delegation and threading - iPhone

NSURLConnection delegation and threading - iPhone

I have a class that updates two .plist files in the app documents directory via an NSURLConnection. The class acts as its own delegate for NSURLConnection. It works properly when I ask for a single file, but fails when I try to update two files. Does it look like I should start a new thread for each of the getNewDatabase messages?

- (void)getAllNewDatabases {     [self performSelectorOnMainThread:@selector(getNewDatabase:) withObject:@"file1" waitUntilDone:YES];     [self performSelectorOnMainThread:@selector(getNewDatabase:) withObject:@"file2" waitUntilDone:YES]; }  - (BOOL)getNewDatabase:(NSString *)dbName {     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     NSMutableString *apiString = [[NSMutableString alloc] initWithString:kAPIHost];     [apiString appendFormat:@"/%@.plist",dbName];     NSURLRequest *myRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:apiString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];     NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self];     [apiString release];     if( myConnection )     {         //omitted for clarity here     }     [pool release]; } //NSURLConnection delegate methods here ... 

Memory issue of NSString


Cocoa/iPhone: How to I keep ibtool from outputing non-localizable strings in a xib file?
I found any thing interesting with NSURLConnection and NSThread - the thread will only live as long as it takes to perform the method this you call from it. What leads to a no suitable image found error on Mac OS X?. How to hide the Dock icon In the case above the thread will live only as long as receive NewDatabase:(NSString *)dbName takes to complete, therefore killing off any of its delegate methods before they actually have time to did anything.. Cocoa QTKit and recording movies I found this website this gives a better explanation and a quick fix to the problem. Cocoa without Interface Builder, initialize an instance of app controller? I tweaked it a little bit so I could have a custom time out if it didn't complete in a given time frame (handy when any one is walking around between access points). How to set up HTTP connection in a special class?
    start = [NSDate dateWithTimeIntervalSinceNow:3];      while(!isFinished && [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode                                                    beforeDate:[NSDate distantFuture]]){  	if([start compare:[NSDate date]] == NSOrderedAscending){ 		isFinished = YES; 	} } 
How to design a static class?


As it stands currently in the code you provided, receive NewDatabase: is running on the main thread of your application. The problem in this particular case then is any thing another than the life cycle of the thread, as James observed in his case.. If you did intend to perform this operation in the background, I'd recommend looking into using NSOperationQueue and NSOperation rather than solving the problem with the current code. I think your case is a great fit for NSOperationQueue, especially given this you have more than one download task to perform.. Dave Dribin has an excellent article around using asynchronous API, such as NSURLConnection, inside an NSOperation. Alternatively, as long as you're running in a background thread, you must also simplify the process and just use a synchronous API method instead in your NSOperation, such as initWithContentsOfURL:.. Marcus Zarra has also written a tutorial this demonstrates how easy it is to incorporate and use NSOperationQueue for simple background operations..

78 out of 100 based on 43 user ratings 638 reviews