如何在 Objective-C 中动态创建实体?
我正在构建一个 iPad 应用程序,我需要用户动态创建实体。我已经拥有程序使用的 3 个实体。
你能帮我写一下代码吗?
我想根据我的理解了解整个结构,我必须创建新的 ManagedObjectModel,添加新实体,然后将其与现有实体合并,这是正确的吗?
I'm building an iPad application where I need user to create entity dynamically. I'm already having 3 entities which program uses.
Could you help me with code how to do it?
I want to understand the whole structure according to my understanding I have to create new managedObjectModel, add new entities and than merge it with existing one, is it correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然在实践中可以动态创建新实体和新模型,但这非常复杂。如果不出意外,您将必须将任何现有的持久数据迁移到新模型和新的持久存储文件。我强烈建议不要尝试这样做,特别是如果您刚刚开始使用核心数据。
您确实有选择:
首先,您确定您确实需要一个新实体吗?刚开始使用 Core Data 的人经常将实体误认为是托管对象。实体之于托管对象就像类之于实例。实体是用于创建对象图的抽象。它们实际上并不包含数据。需要新实体的时候是非常非常罕见的。
其次,如果您确实需要某种动态实体,通常最好将动态实体分解为众多固定的子实体,然后使用关系来创建虚拟实体。例如,您需要一个动态的“人”“实体”,因此您在模型中创建多个实体,每个实体都包含该人的一个属性。您可以有一个
Field
实体,其中包含一个fieldName
属性,然后是一个fieldValue
属性。然后有一个实际的Person
实体,它没有属性,只有与必要的Field
对象的关系。您可以添加任何人所需的任何字段,然后通过遍历其字段的关系来重新构建虚拟人对象。然而,我相当怀疑您是否需要这种灵活性。这种需求是非常罕见的。我会退后一步,看看您认为用户可能需要输入哪些动态数据。
While it is possible to create a new entity and a new model on the fly in practice this is massively complex. If nothing else you would have to migrate any existing persisted data to the new model and a new persistent store file. I strongly recommend against attempting this especially if you are just starting out with Core Data.
You do have options:
Firstly, are you sure you actually need a new entity? People just starting out with Core Data often mistake entities for managed objects. Entities are to managed objects as classes are to instances. Entities are abstractions used to create the object graph. They don't actually contain data. The times when you need new entities are very,very rare.
Secondly, if you do need some kind of dynamic entity, it would usually be best to decompose the dynamic entity into numerous fixed subentities and then use relationships to create a virtual entity. E.g. you need a dynamic Person "entity" so you create several entities in the model each of which holds one attribute of the person. You could have a
Field
entity which would have afieldName
attribute and then afieldValue
attribute. Then have a an actualPerson
entity that has no attributes but just relationships to the necessaryField
objects. You could add any fields needed to any person and then reconstitute an virtual person object by walking the relationships to its fields.I rather doubt however that you need that kind of flexibility. Such a need is very rare. I would step back and see exactly what dynamic data you think the user might need to enter.
这是正确的——您将创建一个 NSEntityDescription 对象数组,然后在新的托管对象模型上调用
setEntities:
。最后,您将该模型与内置模型合并。但请注意,一旦模型被用于创建托管对象上下文(或用于存储),您就无法更改模型。模型更改后,您需要创建新的存储和上下文。
That's correct -- you'd create an array of NSEntityDescription objects, then call
setEntities:
on the new managed object model. Then, finally, you'd merge that model with your built-in model.But note that you can't change a model once it has been used to create a managed object context (or used for storage). You'll need to create new storage and context after the model is changed.