在运行时定制核心数据模型? [英] Customize core data model at runtime?

查看:227
本文介绍了在运行时定制核心数据模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个可以由用户自定义的模型。

I want a model which could be customized by the user. Is it possible with core data or are there better solutions?

感谢matchi

它是一个应用程序的mac os!

Ps: it is an application for mac os!

推荐答案

这是解释在创建托管对象模型 a> Apple的核心数据实用程序教程< a>。一般来说,一旦您拥有对受管对象模型的引用,就可以使用 NSEntityDescription NSAttributeDescription 类自定义实体及其属性对象模型。

This is explained under "Creating the Managed Object Model" of Apple's Core Data Utility Tutorial. In general, once you have a reference to a managed object model, you can use the NSEntityDescription and NSAttributeDescription classes to customize the entities and their attributes in the managed object model.

但是,请注意,在大多数情况下,一旦你修改一个管理对象模型,它将不再与现有的持久性数据存储兼容,这意味着你将必须将数据从旧的持久存储迁移到新的。

Note, however, that in most cases once you modify a managed object model it will no longer be compatible with existing persistent data stores, meaning that you will then have to migrate data from your old persistent store to your new one. This is definitely not an endeavor to be taken lightly.

当然,正如注释中提到的,Core Data也可以自动迁移数据,这个过程称为轻量级迁移。一般来说,这样做

Of course, as mentioned in the comments, Core Data can also migrate data automatically, a process known as lightweight migration. In general, though, to do so


核心数据需要能够找到
源和目标管理对象
在运行时模型本身。 (核心数据
搜索
NSBundle allBundles 和$ b $返回的包b allFrameworks 方法。)然后必须
分析对
持久实体和属性的模式更改,
生成推断的映射模型。
为了使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:


  • 简单添加新属性

  • 非可选属性变为可选

  • 可选属性变为非可选属性,定义默认值

这是否适合您的用例,以更改管理对象模型的方式,使轻量级迁移不可能?

Does this fit your use case, or do you want to allow your users to change the managed object model in ways that would make lightweight migration impossible?

在任何情况下,我强烈建议您阅读以下文档,用户修改Core Data模型。

In any case, I highly recommend that you read through the following documents before you try to allow your users to modify Core Data models.

  • Core Data Programming Guide
  • Core Data Model Versioning and Data Migration Programming Guide
  • Core Data Utility Tutorial
  • NSPersistentDocument Core Data Tutorial

这篇关于在运行时定制核心数据模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆