OGeek|极客世界-中国程序员成长平台

标题: ios - CoreData合并冲突与单个上下文 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:57
标题: ios - CoreData合并冲突与单个上下文

我无法解决一个看起来太愚蠢而无法存在的问题。我有一个使用 CoreData 生成对象的 UITableView。

我在 [context save] 处返回错误:

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]; 的新调用返回另一个 View 并像这样保存它:

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

-(void)viewWillDisappearBOOL)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 对同一数据进行操作时会发生此错误。但我一定错过了一些基本的东西,因为我不相信我有两个上下文。我不是在一个上下文中简单地创建、修改和删除对象吗?



Best Answer-推荐答案


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

关于ios - CoreData合并冲突与单个上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24847500/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4