CoreData INSERT or REPLACE?

I am currently porting an iphone application using SQLite in CoreData.

I need to do INSERT or REPLACE in order to add only new content, is there a way to do this or do I need to get the whole database and look for existing objects and add new ones?

Thanks.

+6
sqlite iphone cocoa-touch cocoa core-data
source share
3 answers

Remember that Core Data is a hierarchy of objects, which, as always, is stored in the database, so you need to look at it as a graph of objects and not a database.

So yes, you need to check if an object already exists using a specific specific unique identifier, and if it does not currently exist, you need to create a new object or update an existing object.

Update

You do not need to retrieve all the objects; look for storage using NSFetchRequest and NSPredicate to check for availability; if it exists, if it is not created.

+14
source share

See this Apple document entitled Efficient Data Import. This is exactly the topic you would like to know. In particular, carefully read the section "Implementing a Search or Creation Effectively".

And, of course, buy a book by Marcus Zarra from the main data, which is fantastic :)

+7
source share

Basically we order inserted (extracted) data and json data, so they have equal values ​​at each index. Then we extract the value in the corresponding index and update the managed object, and in the case when the counter is greater than the selected length of the result, we insert a new managed object.

 //Add JSON objects into entity - (void) insertUpdate:(NSArray *)jsonArrayData { //sort JSON Data NSSortDescriptor *idDescriptor = [[NSSortDescriptor alloc] initWithKey:@"entityID" ascending:YES selector:@selector(localizedStandardCompare:)]; NSArray *sortDescriptors = @[idDescriptor]; NSArray *sortedJSONArray = [jsonArrayData sortedArrayUsingDescriptors:sortDescriptors]; //get entity NSEntityDescription *entity = [NSEntityDescription entityForName:@"entity" inManagedObjectContext:self.managedObjectContext]; // Get the ids from json in sorted order. NSMutableArray *entityIDs = [[NSMutableArray alloc]init]; for (NSDictionary *jsonValues in jsonArrayData) { id value = [jsonValues objectForKey:@"entityID"]; [entityIDs addObject:value]; } //sort to make sure json and fetched data are both sorted in same manner [entityIDs sortUsingSelector:@selector(compare:)]; // create the fetch request to get all Entities matching the IDs NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; [fetchRequest setEntity:entity]; NSString *filter = @"(%K IN %@)"; [fetchRequest setPredicate: [NSPredicate predicateWithFormat: filter, @"entityID", entityIDs]]; // Make sure the results are sorted as well. [fetchRequest setSortDescriptors: @[ [[NSSortDescriptor alloc] initWithKey: idName ascending:YES] ]]; // Execute the fetch. NSError *fetchError; NSArray *entityMatchingNames = [self.elRehabCoreData.managedObjectContext executeFetchRequest:fetchRequest error:&fetchError]; int i = 0; //loop over JSONData for (NSDictionary *keyedValues in sortedJSONArray) { id value = [keyedValues objectForKey:@"entityID"; //Create NSManagedObject NSManagedObject *managedObject = nil; int updateInsert = 0; if(entityMatchingNames.count > i ){ //update managedObject = [entityMatchingNames objectAtIndex:i]; if ([[managedObject valueForKey:@"entityID"] isEqual:[keyedValues valueForKey:@"entityID"]]){ updateInsert = 1; } }else{ //insert managedObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext]; updateInsert = 1; } i++; //set value if updateInsert is true // The updateInsert flag is an extra security to make sure we only update when the value in JSON data is equal that of fetched data if (updateInsert) { [managedObject setValue:value forKey:attribute]; } } //save core data stack [self saveContext]; } 
+3
source share

All Articles