iphone - 通过iOS无法保存核心数据对象

标签 iphone objective-c ios core-data

到目前为止,我已经成功保存了核心数据对象。在这段代码中,我正在搜索一些对象,并想要更新或向已保存的对象添加属性。

显然它不会保存更改,知道为什么吗?

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];
            }
        }
    }
}

谢谢!

最佳答案

如果更改托管对象,则仅在内存中进行更改。更改任何内容后,您需要保存托管上下文。添加如下内容:

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

关于iphone - 通过iOS无法保存核心数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6563839/

相关文章:

iphone - MKMapView上方的IOS activityIndi​​cator subview

iphone - Xcode显示未知字符

objective-c - UIScrollview 倾斜

ios - objective-c 中的类扩展

ios - 如何将一个circleView进度添加到一个UIView并扩展到所有UIView?

iphone - 在自定义尺寸屏幕中播放视频 - 在 iPhone 中查看

iphone - 遍历 NSArray objectAtIndex

iphone - 如何在textField中将文本位置设置为底部

ios - Swift 在字符的第一个匹配处拆分字符串

iphone - UIScrollView 在初始缩放后滚动太远