Background saving with Core Data?

一曲冷凌霜 提交于 2020-01-10 08:26:11

问题


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


回答1:


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!



来源:https://stackoverflow.com/questions/2140342/background-saving-with-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!