Iphone 如何知道managedObjectContext正在另一个视图控制器中更改

Iphone 如何知道managedObjectContext正在另一个视图控制器中更改,iphone,core-data,Iphone,Core Data,我在这里学习核心数据教程。我们有RootViewcontroller和addRecipeViewController 我列出了一些类和函数以及下面的流程屏幕,这样你就不会迷路了 RootViewController.m - (void)insertNewObject { AddRecipeViewController *addRecipeView = [[AddRecipeViewController alloc] initWithNibName:@"AddRecipeViewCon

我在这里学习核心数据教程。我们有RootViewcontroller和addRecipeViewController

我列出了一些类和函数以及下面的流程屏幕,这样你就不会迷路了



RootViewController.m

- (void)insertNewObject {
    AddRecipeViewController *addRecipeView = [[AddRecipeViewController alloc] initWithNibName:@"AddRecipeViewController" bundle:[NSBundle mainBundle]];
    Recipes *recipes = (Recipes *)[NSEntityDescription insertNewObjectForEntityForName:@"Recipes" inManagedObjectContext:self.managedObjectContext];
    addRecipeView.recipes = recipes;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: addRecipeView];
    [self.navigationController presentModalViewController:navController animated:YES];
    [addRecipeView release];
}

流程图:


单击addRecipeViewController的事件
保存
时,它将
配方
保存到
managedObjectContext
中。rootViewConroller迟早会使用
NSFetchedResultsController从
managedObjectContext
检索数据

问题:我不明白如何将
manageObjectContext
用于所有视图控制器,以便在中从
manageObjectContext
添加或删除
Recipe
后,在
rootViewController
处获得最新的
manageObjectContext
addRecipeViewController

请帮我理解这个问题


这里欢迎所有评论。

managedObjectContext基本上是您的持久层,它包括一个缓存和一种检索尚未在缓存中的对象的方法。您希望避免应用程序中存在多个托管对象上下文,这样就不需要处理严重的缓存同步问题

所以我不确定你到底遇到了什么问题导致你停下来,但请不要把问题复杂化。核心数据足够好,可以为您提供持久性存储的单个入口点,并使所有内容保持同步,因此您应该使用它来运行:)


另外,请确保不要混淆
NSManagedObjectContext
NSManagedObject
。托管对象位于上下文中。它们不是同一件事。

当上下文中发生变化时,您可能希望得到通知。如果是这样,请阅读以下内容:

我的问题是在保存配方时,哪个managedObjectContext将存储此对象,以及在检索数据时(显然,数据是配方对象),哪个managedObjectContext用于执行此操作……都是同一个对象。您的应用程序应该只需要一个managedObjectContext。例如,将其存储在AppDelegate中,并提供一种方法从需要的任何位置获取它。
addRecipeViewController.h
@class Recipes;

@interface AddRecipeViewController : UIViewController <UITextFieldDelegate> {
    Recipes *recipes;
    UITextField *textFieldOne;
    UITextField *textFieldTwo;
}
addRecipeViewController.m
    - (void)save {
        1.recipes.recipeName = textFieldOne.text;
        2.recipes.cookingTime = textFieldTwo.text;
        3.NSError *error = nil;
        4.if (![recipes.managedObjectContext save:&error]) {
            // Handle error
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }   
        [self dismissModalViewControllerAnimated:YES];
    } 
- (void)insertNewObject {
    AddRecipeViewController *addRecipeView = [[AddRecipeViewController alloc] initWithNibName:@"AddRecipeViewController" bundle:[NSBundle mainBundle]];
    Recipes *recipes = (Recipes *)[NSEntityDescription insertNewObjectForEntityForName:@"Recipes" inManagedObjectContext:self.managedObjectContext];
    addRecipeView.recipes = recipes;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: addRecipeView];
    [self.navigationController presentModalViewController:navController animated:YES];
    [addRecipeView release];
}