Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would imagine that, since this is such an old question, it's no longer an issue. But as I recently found myself wrestling with a similar situation, I figured I'd offer my solution.</p> <p>I download an array of thousands of objects (which are associated by one or more barcodes) to an XML file, which I then parse and save in batches on a background context using an NSOperation subclass. The disk context (connected to the persistent store) listens for the NSManagedObjectContextDidSaveNotification, at which point it also saves (remember to do so in a block).</p> <pre><code>[self.diskManagedObjectContext performBlockAndWait:^{ NSError *error; if (![self.diskManagedObjectContext save:&amp;error]) { NSLog(@"error saving to disk: %@",error); } }]; </code></pre> <p>The tricky part is that any of those objects' barcodes might be scanned (in the UI context) before the XML file has been fully parsed and the information loaded into the database. And I have a table view backed by an NSFetchedResultsController that displays scanned objects, which show "unknown" in their titles until data is retrieved from the background load. Like in Carl's situation, these objects need to be updated when the background context saves.</p> <p>To handle this, I use a three-context system similar to the one in <a href="https://stackoverflow.com/questions/10542097/implementing-fast-and-efficient-core-data-import-on-ios-5" title="this question">this question</a>, with a UI Context and background context, both of which are children of my main disk context (which is tied to the persistent store). So, during the XML parsing, I run a query on an object's barcodes in the background context (which also pulls from the parent disk context) to see if an object has already been created with that barcode. If it has, I merely update the information, re-save, <em>reset the disk context</em>, and call refreshObject:mergeChanges: from the UI context to pull these changes from the disk context. This successfully refreshes the table without creating duplicates.</p> <p>I originally tried this using only two contexts, a background context and a UI context, but while I never ended up creating duplicate objects, I got really good at hanging the UI thread.</p> <p>I guess the important question is How are you modifying the existing entity? And don't forget the reset. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload