0

First of all I'm using MagicalRecord to manage my Core Data. So right now my database layer works just great and it saves changes every time the change occurs. For example: I'm adding a new entry to a table and it gets written to db file stored on my hard disc right away. What I want to achieve is to keep all the changes in-memory, and write them to db file only on "save" command click.

I figured that the call that does the db file writing is:

[managedObjectContext MR_saveToPersistentStoreAndWait];

So as I figured, I can do all my modifications without calling that method, and then on "save" click, call that method. However, it works only if the thread wasn't changed. Every time, the thread changes the ManagedObjectContext gets reset or recreated, and I lose all my data.

EDIT: just found method in Magical Record:

[NSPersistentStoreCoordinator MR_coordinatorWithInMemoryStore];

which is what I need for the first part of my problem. Although, I don't know how to change from that type of the coordinator to

[NSPersistentStoreCoordinator MR_coordinatorWithSqliteStoreNamed:objectModelName];

without losing any data.

Does anyone know how to do this right?! Any kind of help is really appreciated!

Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80
  • 1
    possible duplicate of [How do I use Magical Record to create & update objects and save them without using contextForCurrentThread](http://stackoverflow.com/questions/19251246/how-do-i-use-magical-record-to-create-update-objects-and-save-them-without-usi) – Arek Holko Oct 21 '13 at 18:05
  • that post is a bit different from what I am doing here...they are using the same context all the time, but I need to have multiple contexts. – Eugene Gordin Oct 21 '13 at 18:16

2 Answers2

0

Check these options:

1- There is setupCoreDataStackWithInMemoryStore which might be helpful if you want to run everything in memory.

2-You can save in the background:

[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext){

}];

3-To get the default context or the one for the thread

[NSManagedObjectContext MR_defaultContext];

[NSManagedObjectContext MR_contextForCurrentThread];

Komposr
  • 1,946
  • 1
  • 12
  • 4
0

I figured it out by doing serious research. I need to use

 NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithInMemoryStore];

to have my data "hang" before user clicks on save, where I do the following:

// psc - my current persistentStoreCoordinator; urlForStore - place where I'm gonna store SQLite
[psc migratePersistentStore:[psc persistentStores][0]  toURL:urlForStore options:nil withType:NSSQLiteStoreType error:&error];
Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80