fatbobman / blogComments

1 stars 0 forks source link

如何在 Core Data 中使用 Derived 和 Transient 属性 | 肘子的Swift记事本 #122

Open fatbobman opened 3 years ago

fatbobman commented 3 years ago

https://www.fatbobman.com/posts/derivedAndTransient/

使用过 Core Data 的开发者,一定会在编辑 Data Model 时看到过右侧的属性面板中的 Derived 和 Transient 两个属性。关于这两个属性的文档不多,大多的开发者并不清楚该如何使用或在何时使用该属性。文本将结合我的使用体验,对 Derived 和 Transient 两个属性的功能、用法、注意事项等内容作以介绍。

Yesifan commented 2 years ago

非常棒 关于 derived 的内容的确很少 很好的帮助到了我

rshinich commented 1 year ago

肘哥你好,请教一个问题,对于Derived修饰的字段,是不是不适配CoreData+CloudKit的情况?我尝试过添加一个Derived的字段后,收到了这样的报错:“... Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." ... reason=Cannot migrate store in-place: CloudKit integration forbids renaming 'foodServes' to 'foodPortions'. Older devices can't process the new relationships.”

fatbobman commented 1 year ago

肘哥你好,请教一个问题,对于Derived修饰的字段,是不是不适配CoreData+CloudKit的情况?我尝试过添加一个Derived的字段后,收到了这样的报错:“... Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." ... reason=Cannot migrate store in-place: CloudKit integration forbids renaming 'foodServes' to 'foodPortions'. Older devices can't process the new relationships.”

你好。 我没有亲自做过测试,不过理论上 Derived 属性应该可以进行同步。 从错误提示来看,无法迁移的原因是你修改了某个属性名称或 Entity 名称 ( 'foodServes' to 'foodPortions'. ) 在 Core Data with CloudKit(四)—— 调试、测试、迁移及其他 这篇文章中,有关于迁移的注意事项

image
rshinich commented 1 year ago

肘哥你好,请教一个问题,对于Derived修饰的字段,是不是不适配CoreData+CloudKit的情况?我尝试过添加一个Derived的字段后,收到了这样的报错:“... Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." ... reason=Cannot migrate store in-place: CloudKit integration forbids renaming 'foodServes' to 'foodPortions'. Older devices can't process the new relationships.”

你好。 我没有亲自做过测试,不过理论上 Derived 属性应该可以进行同步。 从错误提示来看,无法迁移的原因是你修改了某个属性名称或 Entity 名称 ( 'foodServes' to 'foodPortions'. ) 在 Core Data with CloudKit(四)—— 调试、测试、迁移及其他 这篇文章中,有关于迁移的注意事项 image

好的!谢谢肘哥,我再仔细看看!

kaunteya commented 4 months ago

Hi, Do you think it will be a good idea to to use primitiveValue(forKey:) to set a transient property based on existing model properties?

Like to get fullName

let firstName = primitiveValue(forKey: "firstName") as! String
let lastName = primitiveValue(forKey: "lastName") as! String
setPrimitiveValue("\(firstName) \(lastName)", forKey: "fullName")

Also, if you can add an example in Transient property section that lets you set a transient property based on existing properties, that would be great