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/

相关文章:

ios - 对对象的类方法实例上的运行方法的困惑

iphone - PDF 到 EPUB 转换过程中丢失的图像

objective-c - 调试运行时错误: 'NSInternalInconsistencyException' ,原因: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath'

ios - 重力与 UIview

objective-c - iOS 中与 Cocoa 的 `NSSwitchButton` 复选框最接近的模拟是什么?

iphone - 无需源代码即可集成 iPhone 应用程序

iphone - 使用 UIScrollView 生成问题

将 NSNotificationCenter 添加到 UIView 的 iOS 异常

ios - iPhone 6 上的拉伸(stretch)界面

iphone - 为什么要排空自动释放池而不是释放它?