iOS 核心数据瞬变如何工作?

max*_*wer 2 core-data nsmanagedobject nsmanagedobjectcontext ios

我正在尝试了解 iOS Core 数据瞬态属性,但在理解某些行为时遇到了麻烦。

设置

我有两个上下文,一个主上下文和一个私有上下文。我称它们为 mainContext 和 threadedContext 。

线程上下文是父上下文,主上下文是子上下文。(我这样做是因为我的线程上下文比我的主线程和 UI 更频繁地更改模型。

我有需要通过上下文传递的值的瞬态属性。

我发现有时我失去了价值,有时我不依赖于我如何运行。

样本

此代码已被简化以显示问题。我有一个 Person 对象。Person 对象有一个名为“other”的瞬态实体,您将看到我为其分配了一个 Other 对象,该对象具有几个简单的属性,仅此而已。

- (void)case1
{

NSManagedObjectContext *mainThreadContext = [AppDelegate appDelegate].mainThreadContext;
NSManagedObjectContext *threadedContext = [AppDelegate appDelegate].threadedContext;

__block NSManagedObjectID *objectID = nil;

[mainThreadContext performBlockAndWait:^{
    //create
    Person *aPerson = [self createAPersonOnContext:mainThreadContext];

    //setup
    Other *other = [[Other alloc] init];

    aPerson.other = other;

    aPerson.other.favoriteColor = @"Blue";
    aPerson.other.city = @"Provo";

    //save
    NSError *error = nil;
    [mainThreadContext save:&error];

    objectID = aPerson.objectID;

    NSLog(@"%@",aPerson);

}];    
}
Run Code Online (Sandbox Code Playgroud)

当我像这样检索对象时,仍然设置了 person.other 属性(请注意,我在检索对象后正在保存:

[threadedContext performBlockAndWait:^{
    Person *aPerson = [self getPersonOnContext:threadedContext withID:objectID];

    NSError *threadedError = nil;
    [threadedContext save:&threadedError];

    NSLog(@"threaded %@", aPerson);
}];
Run Code Online (Sandbox Code Playgroud)

当我像这样检索对象时,不再设置 person.other(请注意,我在检索对象之前正在保存)

[threadedContext performBlockAndWait:^{

    NSError *threadedError = nil;
    [threadedContext save:&threadedError];

    Person *aPerson = [self getPersonOnContext:threadedContext withID:objectID];

    NSLog(@"threaded %@", aPerson);
}];
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的方法,包括 refreshObject:mergChanges: 我试图观察对象何时出现故障,但这似乎没有帮助。即使当前没有实例化模型对象,瞬态值是否存储在给定的上下文中(假设我已经保存,或者可能没有给出我看到的问题)?

对于那些觉得他们需要更多的人... getPersonOnContext:WithID 方法如下所示:

- (Person *)getPersonOnContext:(NSManagedObjectContext *)context withID:(NSManagedObjectID *)ID
{
    __block Person *person = nil;
    [context performBlockAndWait:^{
        person = (Person *)[context objectWithID:ID];
    }];
    return person;
} 
Run Code Online (Sandbox Code Playgroud)

createAPersonOnContext: 看起来像这样:

- (Person *)createAPersonOnContext:(NSManagedObjectContext *)context
{
    __block Person *person = nil;
    [context performBlockAndWait:^{
        person = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person"
                                                         inManagedObjectContext:context];
        person.firstName = @"matt";
        person.lastName = @"ZZZ";
    }];
    return person;
}
Run Code Online (Sandbox Code Playgroud)

我只是想隐藏这段代码,以帮助引起人们对它自身问题的关注。

如果你想试验这个,我在 github 上有它:https : //github.com/mcmurrym/CoreDataBehaviors

更新:

看来,当我在使用 ID 检索线程上下文中的对象之前保存时,它是错误的 Person 对象,它破坏了瞬态值。如果我在保存之前在线程上下文中检索对象,则临时值将被保留,因为该对象没有故障。

ado*_*oho 5

最大功率,

瞬态非常简单。它们是在后备存储中始终不存在的属性。因此,您看到它们的事实是因为您正在使用子 MOC 并在外部分配了这些值。为了确保瞬态总是有效的,则需要考虑执行-awakeFromInsert-awakeFromFetch-prepareForDeletion-didTurnIntoFault-willTurnIntoFault方法。

安德鲁