核心数据 - 如何在不同上下文中建立两个对象之间的关系

发布于 2024-12-09 07:13:40 字数 3205 浏览 0 评论 0原文

在我的应用程序中,我有以下 CoreData 模型:Foo 有许多 Bar 实体:Foo <---->>>酒吧。

为了添加新的 Foo 实体,我创建了一个新的 MOC,在其中创建了 Foo 的新实例。这将显示 AddFooViewController。在这里,我可以取消或保存或为这个创建的 Foo 实体添加一个新的 Bar 实体。当我添加新的 Bar 实体时出现问题。

让我详细解释一下:新的 Bar 实体是在全新的 MOC 中创建的。然后,当我决定保存 Bar 实体时,我尝试合并“Add Bar MOC”和“Add Foo MOC”。这应该通过以下代码完成:

- (IBAction)addNewBar {
    BarAddViewController *addViewController = [[BarAddViewController alloc] initWithStyle:UITableViewStyleGrouped];

    NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
    self.addingManagedObjectContext = addingContext;
    [addingContext release];


    [addingManagedObjectContext setPersistentStoreCoordinator:[self.managedObjectContext persistentStoreCoordinator]];

    // Create new bar
    Bar *bar = (Bar *)[NSEntityDescription insertNewObjectForEntityForName:@"Bar"                                                                      inManagedObjectContext:self.addingManagedObjectContext];
    bar.creationDate = [NSDate date];


    addViewController.delegate = self;
    addViewController.bar = bar;

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addViewController];
    [self.navigationController presentModalViewController:navController animated:YES];

    [addViewController release];
    [navController release];
}

// Delegate method :
- (void)barAddViewController:(BarAddViewController *)controller didFinishWithSave:(BOOL)save {    
    // Dismiss the modal view to return to the main list
    [self dismissModalViewControllerAnimated:YES];

    // Save modifications
    if (save) {
        NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
        [dnc addObserver:self
                selector:@selector(addBarControllerContextDidSave:) 
                    name:NSManagedObjectContextDidSaveNotification 
                  object:addingManagedObjectContext];

        // Assign relationship here ???? foo.bars = bar;

        NSError *error;
        if (![addingManagedObjectContext save:&error]) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }

        [dnc removeObserver:self 
                       name:NSManagedObjectContextDidSaveNotification 
                     object:addingManagedObjectContext];
    } else {
        // Remove bar from addingManagedObjectContext
        [self.addingManagedObjectContext deleteObject:controller.bar];

        NSError *error;
        if (![addingManagedObjectContext save:&error]) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }
    }

    // Release the adding managed object context.
    self.addingManagedObjectContext = nil;
}

- (void)addBarControllerContextDidSave:(NSNotification*)saveNotification {   
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:saveNotification];
}

我的问题是我不知道在哪里建立 Foo 和 Bar 之间的关系。我知道我只能在同一上下文中的两个 NSManagedObject 之间建立关系。有人可以帮助我吗?

提前致谢

In my app, I have the following CoreData model : a Foo has many Bar Entities : Foo <---->> Bar.

To add a new Foo entity, I create a new MOC in which I create a new instance of Foo. This displays the AddFooViewController. Here, I can either Cancel or Save or Add a new Bar entity for this created Foo entity. Problem happens when I add a new Bar entity.

Let me explain in detail : the new Bar entity is created in a totally new MOC. Then, when I decide to save the Bar entity, I try to merge both "Add Bar MOC" and "Add Foo MOC". This should be done by this code :

- (IBAction)addNewBar {
    BarAddViewController *addViewController = [[BarAddViewController alloc] initWithStyle:UITableViewStyleGrouped];

    NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
    self.addingManagedObjectContext = addingContext;
    [addingContext release];


    [addingManagedObjectContext setPersistentStoreCoordinator:[self.managedObjectContext persistentStoreCoordinator]];

    // Create new bar
    Bar *bar = (Bar *)[NSEntityDescription insertNewObjectForEntityForName:@"Bar"                                                                      inManagedObjectContext:self.addingManagedObjectContext];
    bar.creationDate = [NSDate date];


    addViewController.delegate = self;
    addViewController.bar = bar;

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addViewController];
    [self.navigationController presentModalViewController:navController animated:YES];

    [addViewController release];
    [navController release];
}

// Delegate method :
- (void)barAddViewController:(BarAddViewController *)controller didFinishWithSave:(BOOL)save {    
    // Dismiss the modal view to return to the main list
    [self dismissModalViewControllerAnimated:YES];

    // Save modifications
    if (save) {
        NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
        [dnc addObserver:self
                selector:@selector(addBarControllerContextDidSave:) 
                    name:NSManagedObjectContextDidSaveNotification 
                  object:addingManagedObjectContext];

        // Assign relationship here ???? foo.bars = bar;

        NSError *error;
        if (![addingManagedObjectContext save:&error]) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }

        [dnc removeObserver:self 
                       name:NSManagedObjectContextDidSaveNotification 
                     object:addingManagedObjectContext];
    } else {
        // Remove bar from addingManagedObjectContext
        [self.addingManagedObjectContext deleteObject:controller.bar];

        NSError *error;
        if (![addingManagedObjectContext save:&error]) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }
    }

    // Release the adding managed object context.
    self.addingManagedObjectContext = nil;
}

- (void)addBarControllerContextDidSave:(NSNotification*)saveNotification {   
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:saveNotification];
}

My problem is that I don't know where to establish the relationship between the Foo and the Bar. I know I can only establish the relationship between two NSManagedObject that are in the same context. Does somebody could help me, please ?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2024-12-16 07:13:40

您需要将两个实体加载到同一托管上下文中才能创建任何类型的关系(尽管我猜您已经弄清楚了!)。

您有一个来自第一个托管对象上下文的 Foo - 将其称为 Foo1。

Foo *foo1 = {your initial Foo};

// Get a foo from the new managed context
NSManagedObjectID fooID = [foo1 managedObjectID];
Foo *foo2 = [addingManagedObjectContext objectWithID:fooID];

您现在可以使用 Foo2 创建关系

You need to load both entities in the same managed context to create any sort of relationship (though I guess you've already figures that out!).

You have a Foo from the first managed object context - call it Foo1.

Foo *foo1 = {your initial Foo};

// Get a foo from the new managed context
NSManagedObjectID fooID = [foo1 managedObjectID];
Foo *foo2 = [addingManagedObjectContext objectWithID:fooID];

You can now use Foo2 to create the relationship

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文