Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like you are talking about find-or-create. Depending on the size of the data set and your performance requirements you can do this a couple of ways:</p> <p>The first way is to fetch your existing Core Data objects and store them in an a dictionary with a unique attribute of the entity serving as the key. Then when you download the new data you can take this key for each parsed object and check your dictionary to find (and optionally update) any existing object, or create a new one:</p> <pre><code>NSArray *fetchedObjects = //Fetch the existing objects; NSMutableDictionary *existingObjects = [NSMutableDictionary dictionary]; for (MyClass *object in fetchedObjects) [existingObjects setObject:object forKey:object.uniqueKey]; //Now iterate through the new data (using JSON as example here) NSDictionary *downloadedItems = //Download data and serialise into JSON for (NSDictionary *item in downloadedItems){ NSString *uniqueValue = [item valueForKey:@"uniqueKey"]; MyClass *object = [existingObjects objectForKey:uniqueValue]; if (object==nil){ object = //Insert new MyClass entity into MOC object.uniqueKey = uniqueValue; //Set any other values you only need to set once } //Set any values you may need to update every time //This would be where to check a modified date attribute } </code></pre> <p>The second way is more sophisticated, and involves less memory overhead. It's described in the Efficiently Importing Data section of the <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html." rel="nofollow noreferrer">Core Data Programming Guide</a>. </p> <p>The guide gives a good start but doesn't offer a complete solution; I attempted my own in an answer here: <a href="https://stackoverflow.com/a/12591745/228981">Basic array comparison algorithm</a></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. This table or related slice is empty.
    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