1

更新: ここでの実際の問題はコア データではなく、新しいビューの viewWillLoad と viewDidLoad の間でオブジェクトが解放されるという、私の不適切なプロパティ宣言 (strong/weak) によるものではないことに注意してください。

私は iOS の Core Data に関する Tim Isted の本に取り組んでおり、今までうまくやっています。新しいviewControllerが2番目のmanagedObjectContext(以下のeditingContext)を使用して、保存する前にテキストフィールドの変更をキャプチャする方法を追跡しようとしています。

- (void)setCurrentPerson:(AWPerson *)aPerson {
    if( !aPerson )
    {
        aPerson = [AWPerson randomPersonInManagedObjectContext:self.editingContext];
    }
    else if( [aPerson managedObjectContext] != self.editingContext ) {
        self.title = @"Edit person";
        aPerson = (id)[self.editingContext objectWithID:[aPerson objectID]];
    }
    [...]
}

この時点で: aPerson = (id)[self.editingContext objectWithID:[aPerson objectID]]; aPerson のデバッガーで説明を出力すると、取得する必要があります

<AWPerson: 0x6b5de70> (entity: Person; id: 0x6b5bb60 <x-coredata://A6EC85F2-81A8-488F-B2E3-F82687C252A2/Person/p1> ; data: {
    dateOfBirth = "1973-11-03 12:53:58 +0000";
    eyeColor = "(...not nil..)";
    firstName = Peter;
    lastName = Dickens;
    yearOfBirth = 1973;

代わりに<fault>、値を置き換えた次のようになります

<AWPerson: 0x6b609d0> (entity: Person; id: 0x6b5bb60 <x-coredata:
//A6EC85F2-81A8-488F-B2E3-F82687C252A2/Person/p1> ; data: <fault>)

何が起こっているのか本当にわかりません。行 aPerson の前に値があり、行の後に値が置き換えられます。どんな助けでも大歓迎です。

ありがとう、スティーブ

4

2 に答える 2

1

私はその本を持っていませんが、助けようとします...

しかし、それは私には間違っていないように見えます。それはまさに私が期待するものと同じように見えます (setCurrentPerson が呼び出されたときに aPerson が別のコンテキストに住んでいると仮定します)。あなたが投稿したコードを見ていきます。たぶん、あなたの質問が何であるかを判断し、その過程で何らかの形で答えを提供できるかもしれません. 私のコメントは、コメントとしてコードに含まれています。

- (void)setCurrentPerson:(AWPerson *)aPerson {
    if( !aPerson )
    {
        // The aPerson object we were given is nil, so get one in the
        // current editingContext.
        aPerson = [AWPerson randomPersonInManagedObjectContext:self.editingContext];
    }
    else if( [aPerson managedObjectContext] != self.editingContext ) {
        // The aPerson object does not live in editingContext.  However, apparently
        // we want to make sure it lives in the editingContext.  Remember, each managed
        // that has been saved will have a permanent object-id.  Any context can use
        // the object-id to fetch an object into its own ManagedObjectContext.
        self.title = @"Edit person";
        aPerson = (id)[self.editingContext objectWithID:[aPerson objectID]];

        // Now, aPerson will point to an object that lives in the MOC editingContext.
        // However, if it was not previously registered in that MOC, it will be returned
        // as a fault.  This does not mean the object is not there, but this MOC has
        // not loaded its data.  As soon as you cal a method that needs the data,
        // it will be faulted into memory.

        // One way, is to access any of its properties.
        NSLog(@"firstName = %@", [aPerson valueForKey:@"firstName"]);

        // You can query an object to see if it is a fault.  NO means it's
        // not a fault, and the properties should all be in memory.  YES, does not mean
        // the data is NOT in memory though... it could be in a cache...

        // You can manually fault the object into memory, but I would
        // suggest you read all the documentation before using this, because it has
        // side effects.  Consider...
        if ([aPerson isFault]) {
            [self.editingContext refreshObject:aPerson mergeChanges:YES];
        }

        // NOTE: In general, you just want the object management system itself
        // to manage faults.  However, if you really want to see that your objects
        // are what they are supposed to be, you can do any of this to examine them.
    }
    [...]
}
于 2012-06-02T22:38:28.617 に答える
0

そのままのコードに問題はありません。

Afaultは、CoreData が、ストアに存在するがまだフェッチされていないオブジェクトへのポインターを表す方法です。

これは主にパフォーマンス上の理由で行われ、Person オブジェクトのプロパティのいずれかにアクセスするとすぐに行われます。コアデータはストアからそのエンティティを検索して取得しfirstNamelastNameNSLog の説明は最初の例で見たものと一致します。 .

于 2012-06-02T23:11:27.577 に答える