Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 751393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:39:42+00:00 2026-05-14T14:39:42+00:00

I am trying to launch a background thread to retrieve XML data from a

  • 0

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.

parseContentsOfResponse

-(void)parseContentsOfResponse {

    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:YES];
    [parser setShouldReportNamespacePrefixes:YES];

    [parser parse];

    NSError *parseError = [parser parserError];
    if (parseError) {
        NSLog(@"%@", parseError);
        NSLog(@"publicID: %@", [parser publicID]);
        NSLog(@"systemID: %@", [parser systemID]);
        NSLog(@"line:%d column:%d", [parser lineNumber], [parser columnNumber]);
    }

    ready = YES;
}

First parse section

Each section creates element strings when its elementStart is signaled. The elementEnd will add the object to the dictionary and release the element. The remaining details are redundant and I think the point to note is that the allocations are not directed at an NSZone, therefore they should be residing in the thread’s memory pool.

- (void)parserDidStartDocument:(NSXMLParser *)parser {
    NSLog(@"%s", __FUNCTION__);
    currentChars    = [NSMutableString stringWithString:@""];
    elementQuestion = [NSMutableString stringWithString:@""];
    elementAnswer   = [NSMutableString stringWithString:@""];
    elementKeyword  = [NSMutableString stringWithString:@""];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:39:43+00:00Added an answer on May 14, 2026 at 2:39 pm

    The simplest thing to do would be to create the dictionary in the separate thread, then set it as a property on the main thread, like so:

    - (void)spawnRequestThread: (NSURL*) url  {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        //do stuff with dict
        [self performSelectorOnMainThread:@selector(doneWithThread:) withObject:dict waitUntilDone:NO];
    }
    
    - (void)doneWithThread:(NSDictionary *)theDict {
        self.dict = theDict; //retaining property, can be an NSDictionary
    }
    

    Do you need to change the contents of the dictionary over time? If so, allocating on the main thread and changing the contents in the other thread is possible, but you have to worry about thread-safety issues–NSMutableDictionary isn’t thread-safe, so you’d have to use an atomic property and locks:

    //.h
    @property (retain) NSMutableDictionary *dict; //don't use "nonatomic" keyword
    @property (retain) NSLock *dictLock;
    
    //.m
    - (id) init {
        //blah blah
        dict = [[NSMutableDictionary alloc] init];
        dictLock = [[NSLock alloc] init];
        return self;
    }
    
    - (void)spawnRequestThread: (NSURL*) url  {
        //whenever you have to update the dictionary
        [self.dictLock lock];
        [self.dict setValue:foo forKey:bar];
        [self.dictLock unlock];
    }
    

    Locking is quite expensive and inefficient in any case, so I’d tend to prefer the first method (I’m not sure which is more expensive, exactly, but the first is simpler and avoids thread-safety issues).

    Also, looking at your code, it looks like your NSXMLParser is an ivar which you directly access. This is a bad idea, since NSXMLParser isn’t thread-safe–I would recommend implementing it as a local variable instead. If you do need it as an ivar, use an atomic property and locks and only access it through accessors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Because you have a syntax error (<td>s cannot be directly… May 15, 2026 at 2:00 pm
  • Editorial Team
    Editorial Team added an answer I am not familiar with Cherokee, but the regex would… May 15, 2026 at 2:00 pm
  • Editorial Team
    Editorial Team added an answer jQuery(document).ready(function () { if (jQuery('#domainsAvailable').val() == '') { jQuery('#domainsAvailable').hide(); }… May 15, 2026 at 2:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.