Search code examples
core-datamagicalrecord-2.1

Best practice for managing context saves with MagicalRecord?


I just downloaded the latest version (Developer) from MagicalRecord to fix a few warnings I got when validating my app's latest update.

I now find that MR_saveNestedContexts is deprecated, and is replaced by MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error).

This seems to be a lot of code to write to save each change/addition to the Core Data store; I have 17 different places in my app where I use MR_saveNestedContexts. My thoughts were to create a method and pass it the parameters, based on this article from Loriot.

While thinking about this, I was wondering if there was such a thing as "best practices" for MR?


Solution

  • I cannot give general advice how to handle Core Data save errors. Depending on the data and the application, it might make sense to report a failure to the user, or display a "failed" status in some UI element, or just NSLog it.

    In the simplest case, you could replace MR_saveNestedContexts by MR_saveToPersistentStoreWithCompletion:nil, and use the MagicalRecord error handling methods (setErrorHandlerTarget:action:) to report or log the error.