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

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

+(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;
}

But it causes errors:

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
....

The model seems to be read-only.

+5
4

: ( )

( ). . , . - , . . , , , ,, .

+6

Apple Documentation, :

, . . ( , ), (, ) . : - . , , , .

+2

NSManagedObjectModel:

( ). . , . - , . . , , , , .

, , .

+2
source

Please ignore CoreData, if you want to dynamically create entities, try SQLite (use the FMDB magic library), this will not let you down.

Please note that SQLite / FMDB does not support iCloud.

0
source

All Articles