• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - iOS 核心数据 transient 如何工作?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 23:49:11 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试了解 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap