0

ここでコアデータのチュートリアルに従っています。RootViewcontroller と addRecipeViewController があります。

迷子にならないように、いくつかのクラスと関数、およびフローの画面を以下にリストします。


Recipe.h
#import <CoreData/CoreData.h>
@interface Recipes :  NSManagedObject  
{
}
@property (nonatomic, retain) NSString * recipeName;
@property (nonatomic, retain) NSString * cookingTime;

@end

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];
    } 

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];
}

流れの絵: ここに画像の説明を入力


SaveaddRecipeViewController のイベントをクリックすると、 に保存recipesされ managedObjectContextます。遅かれ早かれ、rootViewConroller は以下を使用してデータを取得しますmanagedObjectContextNSFetchedResultsController

QUESTIONmanageObjectContext :すべてのビュー コントローラーでどのように同じであるmanageObjectContextrootViewControllerRecipeわかりませんmanageObjectContextaddRecipeViewController

この問題を理解するのを手伝ってください。

ここではすべてのコメントを歓迎します。

4

2 に答える 2

1

managedObjectContext は基本的に永続化レイヤーであり、キャッシュと、まだキャッシュにないオブジェクトを取得する方法が含まれています。厄介なキャッシュ同期の問題に対処する必要がないように、アプリに複数のマネージド オブジェクト コンテキストを持たないようにしたい。

そのため、どのような問題が発生して一時停止しているのか正確にはわかりませんが、問題を複雑にしすぎないようにしてください。Core Data は、永続ストアへの単一のエントリ ポイントを提供し、すべての同期を維持するのに十分優れているため、それを使用して実行する必要があります :)

また、 と 混同しないように注意してNSManagedObjectContextくださいNSManagedObject。管理対象オブジェクトはコンテキスト内に存在します。それらは同じものではありません。

于 2012-05-31T16:00:51.340 に答える
0

コンテキストで何かが変更されたときに通知を受け取りたいと思うでしょう。もしそうなら、これを読んでください:コアデータのコールバックメソッドはありますか?

于 2012-05-31T16:03:42.087 に答える