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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:38:06+00:00 2026-06-03T00:38:06+00:00

I have a catalogue application in iOS 5 that is downloading data from XML

  • 0

I have a catalogue application in iOS 5 that is downloading data from XML and displaying them in UITableView using table view controllers with NSFetchedResultsController. Data are stored in Core Data UIManagedDocument. Because I don’t want to block the main queue when downloading and importing data, I have created a background queue for downloading data and new child NSManagedObjectContext with NSPrivateQueueConcurrencyType for importing the data with document.managedObjectContext as a parent. When I finish importing data I -save: changes in the child context and the changes get propagated to parent context. When browsing the catalogue I import additional data when needed. Everything is working fine until the UIManagedDocument gets auto-saved.

I have turned on core data SQL debugging with -com.apple.CoreData.SQLDebug 1 to see when the document is auto-saved.

After document’s auto-save objects with duplicate IDs are being created in document.managedObjectContext (all my entities id database have unique id parameter).

What am I doing wrong?


I have created a simple sample code to reproduce the problem.
Here is the code: http://dl.dropbox.com/u/20987346/ViewController.m
Here is the complete Xcode project: http://dl.dropbox.com/u/20987346/CoreDataTest.zip

Below is the method that is doing the import in the background.

- (void)backgroundImport
{
    static int counter;

    NSManagedObjectContext *backgroundContext;
    backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    backgroundContext.parentContext = self.document.managedObjectContext;

    [backgroundContext performBlock:^{
        NSManagedObject *entity;

        for (int i = 0; i < 2; i++) {
            entity = [self entityWithID:[NSNumber numberWithInt:arc4random() % 20 + 1]
                 inManagedObjectContext:backgroundContext];
            [entity setValue:[NSString stringWithFormat:@"A Name %d", ++counter] forKey:@"name"];
        }

        [self dumpEntitiesInManagedObjectContext:backgroundContext];

        NSError *error;
        [backgroundContext save:&error];
        if (error) NSLog(@"%@ (%@)", [error localizedDescription], [error localizedFailureReason]);

        [backgroundContext.parentContext performBlock:^{
            [self dumpEntitiesInManagedObjectContext:backgroundContext.parentContext];
        }];
    }];
}

The method imports two entities. The -entityWithID: fetches an entity with the specified ID attribute and if it does not exist it creates one using NSEntityDescription -insertNewObjectForEntityForName:. The -dumpEntitiesInManagedObjectContext: dumps all entities to log (once in the import context, once in the document’s context).

The problem is that when the document is auto-saved and some additional importing is done, I get the following in the log:

[1140b] Entities: 10             [fb03] Entities: 11
[1140b]   2: A Name 1            [fb03]   2: A Name 1
[1140b]   3: A Name 4            [fb03]   3: A Name 4
[1140b]   4: A Name 8            [fb03]   4: A Name 8
[1140b]   5: A Name 12           [fb03]   5: A Name 12
[1140b]   6: A Name 10           [fb03]   6: A Name 10
[1140b]   8: A Name 6            [fb03]   8: A Name 6
[1140b] **12: A Name 11**        [fb03] **12: A Name 11**
[1140b]   13: A Name 9           [fb03] **12: A Name 5**
[1140b]   17: A Name 3           [fb03]   13: A Name 9
[1140b]   18: A Name 2           [fb03]   17: A Name 3
                                 [fb03]   18: A Name 2

The import context has 10 entities, but the main context has 11 entities and entity with ID 12 is a duplicate. Seems that the old object has not been modified in parent context but added instead.

  • 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-03T00:38:08+00:00Added an answer on June 3, 2026 at 12:38 am

    I’m still in the thick of all this kind of stuff (Core Data & UIManagedDocument working together) but I think this question might address your situation: Core Data managed object does not see related objects until restart Simulator

    It involves forcing temporary ids to be permanent before the “normal” flow using:
    [context obtainPermanentIDsForObjects:[inserts allObjects] error:&error]

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

Sidebar

Related Questions

I currently have an iPhone app that reads data from an external XML file
In my asp.net mvc application I'm using nhibernate 3.2 as ORM. I have catalogue
Does anyone have a suggested methodology for visualising the data in a star catalogue
Im using c# .net windows form application. I have created a database which has
I have to consume a feed XML (RSS) in my Windows Phone 7 application
I am using VS2005 ASP.NET 2.0. I have a web application which uses Active
I have a MEF (Microsoft Extension Framework) application which loads some assemblies from a
I have an assembly that contains classes that import a number of classes from
I have a reference application that was working and now is broke and I
I have an application that presents a sort of catalog of items in a

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.