1

更新:请注意,这里的实际问题更多不是核心数据,而是我的错误属性声明​​ - 强/弱 - 导致对象在新视图的 viewWillLoad 和 viewDidLoad 之间被释放。

我正在阅读 Tim Isted 关于 iOS 中的核心数据的书,到目前为止一直做得很好。我正在尝试遵循新的 viewController 如何使用第二个 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 对象的任何属性,即firstNamelastName。Coredata 将从存储中搜索并检索该实体,然后您的 NSLog 描述将匹配您在第一个示例中看到的内容.

于 2012-06-02T23:11:27.577 回答