OGeek|极客世界-中国程序员成长平台

标题: ios - iOS 核心数据 transient 如何工作? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:49
标题: ios - iOS 核心数据 transient 如何工作?

我正在尝试了解 iOS Core 数据 transient 属性,但无法理解某些行为。

设置

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

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

我有需要通过上下文传递的 transient 属性。

我发现有时我会失去值(value),有时我并不依赖于我的运作方式。

示例

此代码已被简化以显示问题。我有一个 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 = @"rovo";

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

    objectID = aPerson.objectID;

    NSLog(@"%@",aPerson);

}];    
}

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

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

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

    NSLog(@"threaded %@", aPerson);
}];

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

[threadedContext performBlockAndWait:^{

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

    Person *aPerson = [self getPersonOnContext:threadedContext withIDbjectID];

    NSLog(@"threaded %@", aPerson);
}];

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

对于那些觉得自己需要更多... getPersonOnContext:WithID 方法如下所示:

- (Person *)getPersonOnContextNSManagedObjectContext *)context withIDNSManagedObjectID *)ID
{
    __block Person *person = nil;
    [context performBlockAndWait:^{
        person = (Person *)[context objectWithID:ID];
    }];
    return person;
} 

createAPersonOnContext: 看起来像这样:

- (Person *)createAPersonOnContextNSManagedObjectContext *)context
{
    __block Person *person = nil;
    [context performBlockAndWait:^{
        person = (Person *)[NSEntityDescription insertNewObjectForEntityForName"erson"
                                                         inManagedObjectContext:context];
        person.firstName = @"matt";
        person.lastName = @"ZZZ";
    }];
    return person;
}

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

如果你想对此进行实验,我在 github 上有它:https://github.com/mcmurrym/CoreDataBehaviors

更新:

看来,当我在使用 ID 检索线程上下文中的对象之前保存时,它会使 Person 对象出错,从而破坏了 transient 值。如果我在保存之前在线程上下文中检索对象,则会保留 transient 值,因为该对象没有出现故障。



Best Answer-推荐答案


最大功率,

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

安德鲁

关于ios - iOS 核心数据 transient 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13467195/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4