Memory allocation in detached NSThread to load an NSDictionary in background?

Posted by mobibob on Stack Overflow See other posts from Stack Overflow or by mobibob
Published on 2010-04-24T04:57:17Z Indexed on 2010/04/24 5:03 UTC
Read the original article Hit count: 337

I am trying to launch a background thread to retrieve XML data from a web service. I developed it synchronously - without threads, so I know that part works. Now I am ready to have a non-blocking service by spawning a thread to wait for the response and parse.

I created an NSAutoreleasePool inside the thread and release it at the end of the parsing. The code to spawn and the thread are as follows:

Spawn from main-loop code:

 .
 .
 [NSThread detachNewThreadSelector:@selector(spawnRequestThread:) 
                          toTarget:self withObject:url];
 .
 .

Thread (inside 'self'):

-(void) spawnRequestThread: (NSURL*) url  {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 parser = [[NSXMLParser alloc] initWithContentsOfURL:url];

 [self parseContentsOfResponse];

 [parser release];
 [pool release];
}

The method parseContentsOfResponse fills an NSMutableDictionary with the parsed document contents. I would like to avoid moving the data around a lot and allocate it back in the main-loop that spawned the thread rather than making a copy. First, is that possible, and if not, can I simply pass in an allocated pointer from the main thread and allocate with 'dictionaryWithDictionary' method? That just seems so inefficient.

Are there perferred designs?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about nsautoreleasepool