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?
我正在构建一个iPad应用程序,需要用户动态创建实体。我已经有了3个程序使用的实体。你能帮我写代码吗?
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?
我想要理解整个结构,根据我的理解,我必须创建新的managedObjectModel,添加新的实体,而不是将它与现有的合并,是正确的吗?
2 个解决方案
#1
2
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.
虽然在实践中可以动态地创建一个新实体和一个新模型,但这是非常复杂的。如果没有其他条件,您将不得不将任何现有的持久性数据迁移到新模型和新的持久性存储文件中。我强烈建议不要尝试这种方法,尤其是当您刚刚开始使用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.
首先,你确定你真的需要一个新的实体吗?刚开始使用Core Data的人常常会将实体误认为托管对象。实体对于管理对象就像类对于实例一样。实体是用于创建对象图的抽象。它们实际上不包含数据。需要新实体的时候非常非常罕见。
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 a fieldName
attribute and then a fieldValue
attribute. Then have a an actual Person
entity that has no attributes but just relationships to the necessary Field
objects. You could add any fields needed to any person and then reconstitute an virtual person object by walking the relationships to its fields.
其次,如果您确实需要某种动态实体,通常最好将动态实体分解为许多固定的子实体,然后使用关系创建虚拟实体。例如,您需要一个动态的Person“实体”,因此您可以在模型中创建多个实体,每个实体持有一个人的一个属性。你可以有一个字段实体,它有一个fieldName属性,然后是fieldValue属性。然后有一个实际的Person实体,该实体没有属性,只有与必需的字段对象的关系。您可以向任何人添加所需的任何字段,然后通过将关系遍历到其字段,重新构建虚拟人对象。
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.
但我很怀疑你是否需要这种灵活性。这种需要是非常罕见的。我将后退一步,看看您认为用户可能需要输入哪些动态数据。
#2
2
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.
这是正确的——您将创建一个NSEntityDescription对象数组,然后调用setEntities:在新的托管对象模型上。然后,最后,将该模型与内置模型合并。
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.
但是请注意,一旦模型被用于创建托管对象上下文(或用于存储),您就不能更改它。您需要在模型更改后创建新的存储和上下文。
#1
2
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.
虽然在实践中可以动态地创建一个新实体和一个新模型,但这是非常复杂的。如果没有其他条件,您将不得不将任何现有的持久性数据迁移到新模型和新的持久性存储文件中。我强烈建议不要尝试这种方法,尤其是当您刚刚开始使用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.
首先,你确定你真的需要一个新的实体吗?刚开始使用Core Data的人常常会将实体误认为托管对象。实体对于管理对象就像类对于实例一样。实体是用于创建对象图的抽象。它们实际上不包含数据。需要新实体的时候非常非常罕见。
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 a fieldName
attribute and then a fieldValue
attribute. Then have a an actual Person
entity that has no attributes but just relationships to the necessary Field
objects. You could add any fields needed to any person and then reconstitute an virtual person object by walking the relationships to its fields.
其次,如果您确实需要某种动态实体,通常最好将动态实体分解为许多固定的子实体,然后使用关系创建虚拟实体。例如,您需要一个动态的Person“实体”,因此您可以在模型中创建多个实体,每个实体持有一个人的一个属性。你可以有一个字段实体,它有一个fieldName属性,然后是fieldValue属性。然后有一个实际的Person实体,该实体没有属性,只有与必需的字段对象的关系。您可以向任何人添加所需的任何字段,然后通过将关系遍历到其字段,重新构建虚拟人对象。
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.
但我很怀疑你是否需要这种灵活性。这种需要是非常罕见的。我将后退一步,看看您认为用户可能需要输入哪些动态数据。
#2
2
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.
这是正确的——您将创建一个NSEntityDescription对象数组,然后调用setEntities:在新的托管对象模型上。然后,最后,将该模型与内置模型合并。
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.
但是请注意,一旦模型被用于创建托管对象上下文(或用于存储),您就不能更改它。您需要在模型更改后创建新的存储和上下文。