coc*_*ner 1 iphone core-data objective-c
我有这个代码:
-(void)didEditCard:(NSMutableArray*)theArray {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObject *newManagedObject = [[self fetchedResultsController] objectAtIndexPath:theSIndePath];
// If appropriate, configure the new managed object.
[newManagedObject setValue:[theArray objectAtIndex:0] forKey:@"frontCard"];
[newManagedObject setValue:[theArray objectAtIndex:1] forKey:@"flipCard"];
}
Run Code Online (Sandbox Code Playgroud)
我知道在表视图中触摸了哪个托管对象.如果玩家触摸一个单元格,则会出现一个视图,他可以编辑这些条目.如果他点击完成,那么将调用上述方法.BTW什么都没有.但是,如果我调用setValue,我将不会更新核心数据.
我的目标是:我想更新一个托管对象,该对象应该更新核心数据文件,或者我想从核心数据文件中删除该对象,然后在完全相同的位置添加新对象.
有谁可以帮助我吗?
编辑:
我试过这个:但它不起作用:(它没有得到更新)
-(void)didEditCard:(NSMutableArray*)theArray {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObject *managedObject = [[self fetchedResultsController] objectAtIndexPath:theSIndePath];
// If appropriate, configure the new managed object.
[managedObject setValue:[theArray objectAtIndex:0] forKey:@"frontCard"];
NSManagedObjectContext *context = [[self fetchedResultsController] managedObjectContext];
// 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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
Run Code Online (Sandbox Code Playgroud)
似乎很晚才回答,但对于仍然面临这个问题的少数人来说,以下说明应该会有所帮助.
您可以尝试使用coredata提供的以下方法刷新对象,
- (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag;
Run Code Online (Sandbox Code Playgroud)
或者您可以从数据库中再次查询记录并使用它.这应该可以解决您的问题.
很重要的一点:
从沙箱(模拟器工作目录,如果您在模拟器上测试)打开sqlite,并观察您实际反映的更改.
| 归档时间: |
|
| 查看次数: |
6912 次 |
| 最近记录: |