0

I'm trying to use configurations to manage transient entities in core data.

  • First, i have created 2 coredata configurations named "Persistent" and "Transient".
  • Then, i have created the entities and i have associated them with the right configuration depending on the entity is persistable or not.
  • Finally, using the persistentStoreCoordinator, i have created one persistent store of type Sqlite and linked it to the "Persistent" configuration. I have also created a persistent store of type memory and linked it to the "Transient" configuration.

The test: i start the iphone simulator, my app starts successfully. I close the simulator and start sqlite3 on the database file. I list the tables ( .tables command) and what i can see is that some tables have been created for my transient entities => So, it doesnot work.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    NSLog(@"++++++++ persistentStoreCoordinator");

    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *persistStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ProjectToDelete.sqlite"];

    NSMutableDictionary *sqliteOptions = [NSMutableDictionary dictionary];
    [sqliteOptions setObject:@"WAL" forKey:@"journal_mode"];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                             sqliteOptions, NSSQLitePragmasOption,
                             nil];

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"Persistent" URL:persistStoreURL options:options error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:@"Transient" URL:[NSURL URLWithString:@"memory://store"] options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}

2 Answers2

0

I think you are mixing different concepts of CoreData.

CoreData is not an ORM, rather an object graph which manages your objects and their relationships.

You can have transient properties on an entity, which will not be made into columns in the table representing that entity, only generated on runtime, while the object is in memory.

Abstract entities are never meant to be instanced, they are typically used if you have a lot of objects that inherit from one superobject, i.e. a employee-management system might have an abstract Person-entity, and Employee, Manager, Director and Manager that all inherits from Person

Audun Kjelstrup
  • 1,430
  • 8
  • 13
  • This web page talks about differents ways to handle transient entities : http://www.cimgf.com/2011/08/08/transient-entities-and-core-data but i'm trying to do it using configurations in coredata. – Laurent Michenaud Mar 01 '13 at 07:44
  • 1
    You can't do it with configurations alone. The concept he talks about with transient entities is managed in code, where he creates NSManagedObjects in a NSManagedObjectContext, but never save them. There are a few other ways to achive this, but all of them are done in code, none in the configuration-file – Audun Kjelstrup Mar 01 '13 at 07:53
  • I'm interested in the other ways, if u can talk about them a little more. And for u, what is for you the best way ? – Laurent Michenaud Mar 04 '13 at 07:38
0

As mentioned in a comment, it's not going to work by trying to configure your data store through the model editor. The configurations may let you add predetermined fetches, but they don't let you configure where there data is stored. This is the key to using any of those methods. You will need to add some bits of code, as mentioned in the blog post, to properly use any of those techniques.

casademora
  • 67,775
  • 17
  • 69
  • 78