gpt4 book ai didi

ios - 如何通过核心数据模型动态创建新实体(表)?

转载 作者:可可西里 更新时间:2023-11-01 03:42:50 24 4
gpt4 key购买 nike

我想在 SQLite 中创建一个新实体(表)。我的代码如下:

+(BOOL)CreateDataSet:(NSManagedObjectModel *) model  
attributes:(NSDictionary*)attributes
entityName:(NSString*) entityName
{
NSEntityDescription *entityDef = [[NSEntityDescription alloc] init];

[entityDef setName:entityName];
[entityDef setManagedObjectClassName:entityName];
[model setEntities:[NSArray arrayWithObject:entityDef]];
NSArray *properties = [CoreDataHelper CreateAttributes:attributes];
[entityDef setProperties:properties];

[entityDef release];

return TRUE;
}

但它会抛出错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't modify an immutable model.'*** Call stack at first throw:(    0   CoreFoundation                      0x01c5abe9 __exceptionPreprocess + 185    1   libobjc.A.dylib                     0x01daf5c2 objc_exception_throw + 47    2   CoreData                            0x0152634a -[NSManagedObjectModel(_NSInternalMethods) _throwIfNotEditable] + 106    3   CoreData                            0x01526904 -[NSManagedObjectModel setEntities:] + 36....

这似乎表明该模型是只读的。

最佳答案

From the documentation: (重点是我的)

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.

关于ios - 如何通过核心数据模型动态创建新实体(表)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5341815/

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