开发者

A Second In-Memory-Store As A Cache

Dear community. I try to discover opportunity to using 2 persistent stores for improve performance of my application. What i do here: CREATE 2 PERSISTENT STORES

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNumber numberWithBool:YES] 
         forKey:NSMigratePersistentStoresAutomaticallyOption];

if (![persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType
                                              configuration:nil 
                                                        URL:[NSURL URLWithString:@"memory://store"]
                                                    options:dict 
                                                      error:&error])
{
    [[NSApplication sharedApplication] presentError:error];
    [persistentStoreCoordinator release], persistentStoreCoordinator = nil;
    return nil;
}

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                              configuration:nil 
                                                        URL:url
                                                    options:dict 
                                                      error:&error])
{
    [[NSApplication sharedApplication] presentError:error];
    [persistentStoreCoordinator release], persistentStoreCoordinator = nil;
    return nil;
}

ASSIGN new created objects to in-Memory store

NSManagedObject *objectCarrier = [NSEntityDescription 
                                  insertNewObjectForEntityForName:@"Carrier" 
                                  inManagedObjectContext:managedObjectContext];
[objectCarrier setValue:startForCarrier
                 forKey:@"name"];
NSURL *url = [NSURL URLWithString:@"memory://store"];
[managedObjectContext assignObject:objectCarrier
                        toPersistentStore:[[appDelegate persistentStoreCoordinator] persistentStoreForURL:url]];

SAVE FINAL OBJECT

A difference between in-memory and particular persistent store using is i have wrong using objects from predicates for same code. If i just change persistent store type, i pickup object:

NSManagedObject *destination = [[codeAfterComparing lastObject] valueForKey:codeRelationshipName];            

But set values for this object is doesn't work. If i try to assignObject for received object, i have error (it's doesnt matter, how this object was save as inMemory or asSqlLite store object, error is start every time).

2011-02-16 14:32:45.037 snow server[44411:1803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't 开发者_JAVA百科reassign an object to a different store once it has been saved.'

Attempt to save a final object's graph with two different stores gives me error "CoreData does not support persistent cross-store relationships", and it's doesn't matter, where cureent object assing. Migration was as :

    for (NSPersistentStore *persistentStore in [persistentStoreCoordinator persistentStores]) {
        if (persistentStore.type == NSInMemoryStoreType) {

            // migrate the in-memory store to a SQLite store
            NSError *error;

           [persistentStoreCoordinator migratePersistentStore:persistentStore toURL:[NSURL fileURLWithPath:[[self applicationSupportDirectory] stringByAppendingPathComponent:@"storedata.sql"]] options:nil withType:NSSQLiteStoreType error:&error];
            if (! newPersistentStore) {

Product error: "Can't add the same store twice"

So, the result is a very strange for me: 1. Looks like managed object context have no difference for objects between 2 stores. If i ask save, it take whole object and save so same sqlite store 2. maybe a way to using different persistent store coordinator's but i don't know exactly, how is easy transfer objects between 2 stores. Of course, i can do a copy (include relationships e.t.c.) but this is a hard code for this simple issue, i guess.

Maybe somebody can suggest about my code wrong or good working examples of code to review and understand a good way to do in memory cache with core data? Google search gives not too much examples.


If you look at the Core Recipe example code on Apple's website, they use multiple stores to save objects in memory and on disk.


Thought I'd take a stab here.

I've had this problem in the past. I ended up removing the functionality for two persistent stores in the same coordinator. If I understand Apple correctly, Object Entities cannot be shared between persistent stores. So to make things easier, I usually just do the following (though I suspect there is an efficiency issue with using an additional Coordinator)

  • 1 NSPersistentStore per NSPersistentStoreCoordinator

  • break up the scratchpad work to the NSManagedObjectContexts

  • create a deep-copy method to your NSManagedObject subclasses

And then, when whatever class you have managing each persistent store utilize the copy function to import the managed objects.

I can't really think of an instance where you'd want to go through the extra trouble of individually assigning the managed objects to a specific store that wouldn't be taken car of in this way.

I have a program that utilizes two stores - one in memory for transient objects and another managing the document. It's working just fine.


In iOS 5 Apple introduce Nested Managed Object Contexts where you can work with two Managed Object contexts.

This may replace your approach with the in memory store because e.g. you can now use one of the (also) new concurrency types to run one context in the background (e.g. for background fetching) and another as your main context.

Take a look in the WWDC2011 Session 303.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜