gpt4 book ai didi

objective-c - 在运行时自定义核心数据模型?

转载 作者:太空狗 更新时间:2023-10-30 03:59:45 26 4
gpt4 key购买 nike

我想要一个可以由用户定制的模型。 是否可以使用核心数据或是否有更好的解决方案?

谢谢抹茶

Ps: 是mac os的应用!

最佳答案

这在 "Creating the Managed Object Model" 下进行了解释苹果的Core Data Utility Tutorial .一般来说,一旦你有一个托管对象模型的引用,你就可以使用 NSEntityDescriptionNSAttributeDescription类来自定义托管对象模型中的实体及其属性。

但是请注意,在大多数情况下,一旦您修改了托管对象模型,它将不再与现有的持久性数据存储兼容,这意味着您必须将数据从旧的持久性存储迁移到新的持久性存储。这绝对不是一项可以掉以轻心的努力。

当然,正如评论中所说,Core Data也可以自动迁移数据,这个过程被称为轻量级迁移。不过,一般来说,这样做

Core Data needs to be able to find the source and destination managed object models itself at runtime. (Core Data searches the bundles returned by NSBundle’s allBundles and allFrameworks methods.) It must then analyze the schema changes to persistent entities and properties and generate an inferred mapping model. For Core Data to be able to do this, the changes must fit an obvious migration pattern, for example:

  • Simple addition of a new attribute
  • A non-optional attribute becoming optional
  • An optional attribute becoming non-optional, and defining a default value

这是否适合您的用例,或者您是否希望允许您的用户以无法进行轻量级迁移的方式更改托管对象模型?

无论如何,我强烈建议您在尝试允许您的用户修改 Core Data 模型之前通读以下文档。

关于objective-c - 在运行时自定义核心数据模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6379025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com