我已经使用NSFetchedResultController将数据加载到UITableView中。使用MagicalRecord进行coredata操作。
我在默认上下文中创建了 NSFetchedResultsController ,在 didSelectRowAtIndexPath 上我已经使用了它,如下所示
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UserEntity * user = [userFetchedResultsController objectAtIndexPath:indexPath];
// do some changes to user Object
}
稍后在保存按钮点击操作后,我试图保存如下
[[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
DLog(@" isSaved : %d ",success);
}];
但未保存更改,
2014-09-22 17:12:50.752 _block_invoke [Line 210] has Changed 1
2014-09-22 17:12:50.752 MR_saveWithOptions:completion:](0x7fca7214e7d0) → Saving <NSManagedObjectContext (0x7fca7214e7d0): *** DEFAULT ***> on *** MAIN THREAD ***
2014-09-22 17:12:50.753 MR_saveWithOptions:completion:](0x7fca7214e7d0) → Save Parents? 1
2014-09-22 17:12:50.753 MR_saveWithOptions:completion:](0x7fca7214e7d0) → Save Synchronously? 0
2014-09-22 17:12:50.759 MR_saveWithOptions:completion:](0x7fca7214c430) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
2014-09-22 17:12:50.759 _block_invoke_2 [Line 213] isSaved : 0
显然,NSManagedObject有更改,但是日志显示没有更改,任何人都可以解释这个吗?
更新
我可以在defaultContext的UpdatedObjects列表中看到更改的对象,但仍然没有保存不工作
DLog(@" updated Objects %@ ", [[NSManagedObjectContext defaultContext] updatedObjects]);
updated Objects {(
<UserEntity: 0x1780cd4a0> (entity: UserEntity; id: 0xd000000000040000 <x-coredata://1E0810DA-1A23-4337-B279-3957BE4D207F/UserEntity/p1> ; data: {
// entity data removed
})
)}
NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
任何建议都会非常有用
更新2:
下面是我如何创建NSFetchedResultsController
-(NSFetchedResultsController*) userFetchedResultsController {
@autoreleasepool {
if(userFetchedResultsController == nil){
NSFetchRequest *fetchRequest = [UserEntity requestAllSortedBy:@"userName" ascending:YES];
[fetchRequest setFetchBatchSize:10];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext defaultContext] sectionNameKeyPath:nil cacheName:nil];
userFetchedResultsController = theFetchedResultsController;
userFetchedResultsController.delegate = self;
}
return userFetchedResultsController;
}
}
答案 0 :(得分:-1)
这实际上是我身边的一个逻辑错误,它重置了NSManagedObject值,导致上下文没有变化。
立即更新工作正常。