Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background saving with Core Data?

I have a Core Data based iPhone application that needs to save 1000 managed objects on termination. This takes 8+ seconds, however, the OS kills the app if it does not complete in approx. 6 seconds.

I don't want to save the objects earlier, because then the user has to wait 8 seconds longer for the results to display.

Is it possible to somehow save the objects earlier in a background thread, while still having (read-only) access to the NSManagedObjectContext in the main thread to display the data? Or is it somehow possible to duplicate the managed objects, and pass the duplicates to a background thread for saving?

For clarification, here is what happens in the application now: I have a background thread that imports 1000+ objects in about 1 sec. If I save while importing, it takes a lot longer than 1 sec. So in order to display those items with minimum delay, the context is handed off without saving to the main thread, and the user gets his results as fast as possible.

I now have the problem how to save these objects without the user having to wait the 8 secs. If I save in the background thread before handing over, the user has to wait. If I save in the foreground thread after handing over, the user has to wait. The only two possible approaches I can see right now are:

  1. Somehow having core data doing its sqlite accesses in the background, while still keeping the main thread reactive
  2. Handing the objects unsaved from one context to another, and saving in the background thread

Both approaches seem impossible (at least according to the Core Data documentation). So is there no other solution than to have the user wait longer (and maybe display a nice rotating hour glass :-)?

Regards, Jochen

like image 425
Jochen Avatar asked Jan 26 '10 15:01

Jochen


People also ask

When should you save context in Core Data?

Only Save A Context That Has Changes hasChanges : true if you have inserted, deleted or updated the object (a combination of the following properties). isInserted : true when you insert the object into a context.

Can we use Core Data managed objects from background thread?

Core Data is designed to work in a multithreaded environment. However, not every object under the Core Data framework is thread safe. To use Core Data in a multithreaded environment, ensure that: Managed object contexts are bound to the thread (queue) that they are associated with upon initialization.

What is Core Data on iPhone?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

Is Core Data thread safe a Nsmanagedobjectid?

Core Data Concurrency The NSManagedObjectContext perform(_:) and performAndWait(_:) functions are thread safe and can be called from other threads. A NSManagedObject cannot be shared across threads.


1 Answers

Yes, there's a way to save the managed object context from the background thread, or more precisely, it's usually referred to as "importing in the background thread, and showing in the main thread." This way, the managed objects are saved piece by piece when they are imported, not all at one time at the termination.

I just wrote an short answer on a similar question here at SO, but you should read this Apple doc. There're many potential pitfalls, so read very, very carefully. And then read Apple's "Efficiently Importing Data". This is also a must-read! And Marcus Zarra's CoreData book is also helpful.

CoreData multithreading is a bit tricky, but it really pays off. Good luck!

like image 190
Yuji Avatar answered Nov 16 '22 01:11

Yuji