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 8348207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:37:26+00:00 2026-06-09T07:37:26+00:00

I am reading an XML file that basically looks as follows: <teams> <team id

  • 0

I am reading an XML file that basically looks as follows:

<teams>
    <team id = "A1">
        <player>Tom</player>
        <player>Dick</player>
        <player>Harry</player>
    </team>
    <team id = "A2">
        <player>John</player>
        <player>Tom</player>
    </team>
</teams>

I use NSXMLParser and create Core Data entities in the didEndElement delegate method as follows:

if ([elementName isEqualToString:@"player"]) {
    if ([nodeContent length] != 0) {
        player = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:savedContext];            
        player.name = nodeContent;
    }
}

And do something similar for ‘team’ on didStartElement. ‘nodeContent‘ is what I get from foundCharacters. So far so good. Everything works ok. However, in this example, I have a player (Tom) who plays in two teams. This is possible but I do not want two entities of Tom in my Core Data. So, I check whether a player with that name already exists:

    Player *player = nil;

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Player"];
    request.predicate = [NSPredicate predicateWithFormat:@"name = %@", nodeContent];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    NSError *error = nil;
    NSArray *players = [savedContext executeFetchRequest:request error:&error];

    if (!players || ([players count] > 1)) {
        NSLog(@"WTF?!");
    } else if ([players count] == 0) {
        player = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:savedContext];            
        player.name = nodeContent;
    } else {
        player = [players lastObject];
    }            
    [player addPlaysInTeamObject:team];

… and if that is the case, I just use that entity. If not, I create a new one as before.

Now, this all works when I run the parsing synchronously in the foreground, but I do not want to freeze my app while it is downloading stuff. So I put all this in a thread. It sometimes works ok, but occasionally I get the following error on the executeFetchRequest:

*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x8177a90> was mutated while being enumerated.'
*** First throw call stack:
(0x12d3022 0x1784cd6 0x12d2bf1 0x41314 0xd5b9 0xceaa25 0x19118f6 0x191dab0 0xce901d 0xcf8d 0xc3a5 0x1f31330 0x1f32439 0x908b9b24 0x908bb6fe)
terminate called throwing an exception(lldb)

It happens sometimes when I don’t use the (foreground) UI, but always when I do touch any UI element, even if that UI element does not use the data that I am loading in the background. I checked this forum and concluded that it might be caused by the parser filling up the database while at the same time the NSFetchRequest uses the database to search for a certain name. (Although I do not really understand that because all this happens in the same thread.)

Is there anyone who can tell me what I am doing wrong?

Thank you!

–GB

  • 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-06-09T07:37:27+00:00Added an answer on June 9, 2026 at 7:37 am

    Apparently, you cannot access core data entities while another thread is still creating / updating the core data database.

    In this case I ended up separating the XML parsing and the core data creation. So basically, I parse the entire XML stuff and store it in an NSDictionary. After that, I use the algorithm above to find or create the core data entities with that NSDictionary.

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

Sidebar

Related Questions

I'm reading an XML file that looks like this: <?xml version=1.0 encoding=utf-8?> <VehicleList> <Vehicle>
While implementing XML file reading/writing in my application I saw that when I call
I developing a HTML5 Canvas App and it involves reading a xml file that
I have an XML file that looks like this. <Almond> Roasted Almonds,Almond Extract,Almond Sauce,Almond
I'm reading an xml file generated by a 3rd-party application that includes the following:
Is it possible to do this? I'm reading an XML file that has the
When reading a XML file with linq to XML using a XDocument and there
I am reading an xml file using javascript and then I need to submit
I am reading a xml file with: $reader = new XMLReader(); $reader->xml($myXml, NULL, LIBXML_NOWARNIG
I'm reading an xml file using Jaxb, now the contents of the xml file

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.