Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which MagicalRecord save method

I have an NSOperation subclass which is downloading and importing data into CoreData.

Once I've done this I want to save the context and merge it into the default context.

The saving is not done in a block, it just does it synchronously as it's already in a BG thread.

Once I've done the changes which MR save method should I use?

There are dozens of them and not really sure what each one does.

Should I use...

- (void)save:
- (void)saveOnlySelfAndWait;
- (void)saveToPersistentStoreAndWait;

Or should I do all the changes in...

+ (void)saveUsingCurrentThreadContext...

I want it to be synchronous so that I can control the ending of the operation.

At least could someone explain the differences between the different saves.

like image 574
Fogmeister Avatar asked Mar 21 '13 15:03

Fogmeister


1 Answers

I acknowledge that these methods aren't documented very well. However, they follow with the Core Data nested context model fairly well.

  1. With MagicalRecord don't use save: on an NSManagedObjectContext. MagicalRecord has all those extra error handling, logging and completion handlers built in. You want to use those.

  2. You seem to know where data needs to go (from one context to the root). It depends on your hierarchy as to which save method you need to use. If you are only one level deep (ie. in a child context of the defaultContext), saveOnlySelfAndWait will save to the defaultContext. Otherwise, saveToPersistentStoreAndWait will traverse the hierarchy for you, all the way to the data store.

  3. The andWait methods are blocking calls. The calls with completion a handler are not. These are fairly straight forward in their use.

like image 105
casademora Avatar answered Nov 17 '22 09:11

casademora