核心数据多个managedObjectContexts

时间:2012-06-02 21:32:03

标签: ios core-data

更新:请注意,这里的实际问题不在于核心数据更多与我的糟糕属性声明 - 强/弱 - 导致对象在新视图的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行之前有值,在行之后,它们被替换。任何帮助将不胜感激。

谢谢,史蒂夫

2 个答案:

答案 0 :(得分:1)

我没有那本书,但我会尽力帮助......

然而,对我来说并没有错。它看起来就像我期望的那样(假设aPerson在调用setCurrentPerson时生活在不同的上下文中)。我将尝试浏览您发布的代码。也许我可以决定你的问题是什么,并以某种方式在这个过程中提供答案。我的评论包含在代码中,以及评论。

- (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.
    }
    [...]
}

答案 1 :(得分:0)

您的代码没有任何问题。

fault是CoreData表示指向商店中存在但尚未获取的对象的指针的方式。

这主要是出于性能原因而且只要您访问Person对象的任何属性即firstNamelastName等等.Coredata将从商店中搜索并检索该实体然后,NSLog描述将与您在第一个示例中看到的内容相匹配。

相关问题