CoreData Merge与单个Context进行冲突

时间:2014-07-20 04:53:30

标签: ios objective-c uitableview core-data

对于一个看起来太愚蠢的问题,我无处可去。我有一个使用CoreData生成对象的UITableView。

我在[上下文保存]中收到错误:

if (editingStyle == UITableViewCellEditingStyleDelete) {
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

    Stop *stop=[self.fetchedResultsController objectAtIndexPath:indexPath];

    [context deleteObject:stop];


    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.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

当我传递停止对象和上下文(通过对 [self.fetchedResultsController managedObjectContext]的新调用返回; 另一个视图并将其保存为:

        Stop *object= [[self fetchedResultsController] objectAtIndexPath:indexPath];
    [[segue destinationViewController] setStop:object];
    [[segue destinationViewController] setContext:[self.fetchedResultsController managedObjectContext]];

-(void)viewWillDisappear:(BOOL)animated
{
    NSError *error = nil;
    self.stop.locationText=self.textLocationField.text;
    [self.stop.managedObjectContext save:&error];

if (![self.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.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

错误是NSMergeConflict,我理解当多个MOC作用于相同数据时会发生。但我必须错过一些基本的东西,因为我不相信我有两个上下文。我不是简单地在单个上下文中创建,修改和删除对象吗?

2 个答案:

答案 0 :(得分:1)

经过大量的试验和错误后,当你没有方向关系时,你会得到一个NSMergeConflict。因此,如果你有一个有脚的人,你的脚必须指向关系图中的一个人。

答案 1 :(得分:0)

请注意,您无需传递上下文。您可以访问每个managedObjectContext子类提供的属性NSManagedObject。因此

Stop *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSManagedObjectContext *context = object.managedObjectContext;
[context deleteObject:object];
// make sure you delete the table view cell as well
[context save:&error];

和更新

self.stop.locationText = newText;
[self.stop.managedObjectContext save:&error];

无需拨打updatedObjects。你想用这个完成什么?