开发者

Trouble saving Core Data objects via iOS

开发者 https://www.devze.com 2023-03-17 12:36 出处:网络
I was saving core data objects successfully until now. In this code, I\'m sea开发者_Python百科rching for some objects and want to update or add a property to the saved object.

I was saving core data objects successfully until now. In this code, I'm sea开发者_Python百科rching for some objects and want to update or add a property to the saved object.

Apparently it won't save the changes, any idea why?

NSManagedObjectContext *managedObjectContext = ((VentoAppDelegate*) [[UIApplication sharedApplication] delegate]).managedObjectContext;
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:EntityNameString inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];

NSError *error;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
    NSLog(@"Error - Events=nil - %@", [error localizedDescription]);
}
if ([objects count] > 0) {
    for (NSManagedObject* object in objects)    {
        if (distance <= maxDistance) {
            if (bla-bla-bla) {
                [object setValue:[NSNumber numberWithBool:YES] forKey:@"notification"];
                [self saveContext];
            }
        }
    }
}

Thank you!


If you change managed object it is changed only in memory. You need to save managed context after changing anything. Add something like this:

NSError *error;
   if (![object.managedObjectContext save:&error]) {
   // Update to handle the error appropriately.
   NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
   abort();  // Fail
}
0

精彩评论

暂无评论...
验证码 换一张
取 消