Hantok
Hantok

Reputation: 67

Core Data Objective C update content of Entity

I am first time asking question here, sorry, but I can not find similar one. So, I need update data in Entity "City" attribute - @"name". for Example in my Core Data I already have @"New York", @"Boston". And by parsing XML I have NSMutableArray *Cities = (@"New York", @"Boston", @"Los Angeles", @"Washington");

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSString *attributeString = @"name";
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
//save to the TableView
cell.textLabel.text = [[object valueForKey:attributeString] description];
if ((indexPath.row + 1) == numberOfSectionsInTableView && (self.isParsingDone))
        [self.insertNewObjectToCities:nil];
//When coredata updating - tableView is also updating automatically


//Here is just adding new data, but I do not know how to update
- (void)insertNewObjectToCities_translation:(id)sender
{
     NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
NSString *attributeString = @"name";

if (![[self.parseCities.Cities objectAtIndex:i] isEqualToString:[newManagedObject valueForKey:attributeString]])
{
    [newManagedObject setValue:[self.parseCities.Cities objectAtIndex:i] forKey:attributeString];
    NSLog(@"OBBB %@", [self.parseCities.Cities objectAtIndex:i]);
    NSLog(@"dessss %@", [[newManagedObject valueForKey:attributeString] description]);

    i++;

    if (i==[self.parseCities.Cities count]) 
    {
        i = 0;
        return;
    }
    else 
    {
        NSLog(@"valueForKey %@", [newManagedObject valueForKey:attributeString]);
        [self insertNewObjectToCities_translation:nil];
    }
}
else 
{
    NSLog(@"else");
    return;
}

// Save the context.
NSError *error = nil;
if (![context save:&error]) {
    // Replace this implementation with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

}

Upvotes: 0

Views: 2297

Answers (1)

Paul O.
Paul O.

Reputation: 1176

To update a managed object, you first need to fetch it, make any changes to the fields in the fetched NSManagedObject, and then save the context you used to fetch the object. If you call insertNewObjectForEntityForName again, it will insert a new managed object every time, even if it already exists in Core Data.

It's quite slow to fetch a single object every time you need to check and see if a new one needs to be added. You might want to cache the objects you currently have loaded (or their unique identifying field) into an NSArray or NSSet so you can check that for membership, instead.

Upvotes: 1

Related Questions