The right approach to safely store the context of a managed data object in a background thread?

Apple's "Concurrency with Core Data Documentation" reads the following when discussing the use of master data with background threads.

Saving in background thread - error prone

Asynchronous queues and threads do not prevent the application from quitting smoking. (In particular, all NSThread-based threads were β€œseparated” - see the documentation for pthread for complete information β€” and the process only runs until all unlocked threads disappear.)

and in particular:

If you need to save a background thread, you must write additional code so that the main thread does not allow the application to leave until the completion of the entire save operation.

What is the recommended approach to achieve this in an iOS app?

+4
source share
2 answers

In applicationWillTerminate applicationWillTerminate delegates and their associated methods, you need to check if any background threads were unsaved changes and save them before allowing the application to stop working or go to background.

+5
source

I recommend taking a look at using magic notation ( https://github.com/magicalpanda/MagicalRecord/ ). This greatly simplifies the work with basic data on background threads. I recently found this and used it for a project. We have now made a maintenance effort to upgrade many existing applications to use the new Magical Record Core Data wrapper. This saved us from time and frustration for the few weeks we used.

0
source

All Articles