How to dyanmic create a new entity (table) via CoreData model?

前端 未结 4 1458
南笙
南笙 2021-02-10 00:01

I want to create a new Entity (table) in SQLite. My code is as follows:

+(BOOL)CreateDataSet:(NSManagedObjectModel *) model  
    attributes:(NSDictionary*)attri         


        
相关标签:
4条回答
  • 2021-02-10 00:21

    From the documentation for NSManagedObjectModel:

    Editing Models Programmatically

    Managed object models are editable until they are used by an object graph manager (a managed object context or a persistent store coordinator). This allows you to create or modify them dynamically. However, once a model is being used, it must not be changed. This is enforced at runtime—when the object manager first fetches data using a model, the whole of that model becomes uneditable. Any attempt to mutate a model or any of its sub-objects after that point causes an exception to be thrown. If you need to modify a model that is in use, create a copy, modify the copy, and then discard the objects with the old model.

    In other words, edit your managed object model before you set up your persistent store coordinator or managed object context.

    0 讨论(0)
  • 2021-02-10 00:30

    From the documentation: (emphasis is mine)

    Managed object models are editable until they are used by an object graph manager (a managed object context or a persistent store coordinator). This allows you to create or modify them dynamically. However, once a model is being used, it must not be changed. This is enforced at runtime—when the object manager first fetches data using a model, the whole of that model becomes uneditable. Any attempt to mutate a model or any of its sub-objects after that point causes an exception to be thrown. If you need to modify a model that is in use, create a copy, modify the copy, and then discard the objects with the old model.

    0 讨论(0)
  • 2021-02-10 00:38

    Please ignore CoreData if you want to create entities dynamically, try SQLite (please use the magical FMDB library), it won't let you down.

    Please note that SQLite/FMDB does not support iCloud.

    0 讨论(0)
  • 2021-02-10 00:40

    I quote Apple's Documentation which describes it very accurately:

    Entity descriptions are editable until they are used by an object graph manager. This allows you to create or modify them dynamically. However, once a description is used (when the managed object model to which it belongs is associated with a persistent store coordinator), it must not (indeed cannot) be changed. This is enforced at runtime: any attempt to mutate a model or any of its sub-objects after the model is associated with a persistent store coordinator causes an exception to be thrown. If you need to modify a model that is in use, create a copy, modify the copy, and then discard the objects with the old model.

    0 讨论(0)
提交回复
热议问题