核心数据的后台保存? [英] Background saving with Core Data?

查看:90
本文介绍了核心数据的后台保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Core Data的iPhone应用程序,需要在终止时保存1000个托管对象。这需要8 +秒,但是,如果它没有完成在约的操作系统杀死应用程序。 6秒。

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.

我不想早些保存对象,因为用户必须等待8秒才能显示结果。

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

有可能以某种方式在后台线程中保存对象,同时仍然(只读)访问主线程中的NSManagedObjectContext以显示数据?

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?

为了说明,这里是应用程序现在发生的事情:I有一个后台线程,在大约1秒钟内导入1000多个对象。如果我在导入时保存,则需要大于1秒的时间。因此,为了以最小的延迟显示那些项目,上下文被切换而不保存到主线程,并且用户尽可能快地获得他的结果。

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.

我现在有问题,如何保存这些对象,而无需等待8秒。如果我在切换之前保存在后台线程中,用户必须等待。如果我在切换后保存在前台线程中,用户必须等待。我现在可以看到的唯一两种可能的方法是:

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. 不知何故,核心数据在后台进行sqlite访问,主线程反应

  2. 将未保存的对象从一个上下文移动到另一个上下文,并保存在后台线程中

这两种方法似乎都不可能(至少根据Core Data文档)。

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 :-)?

注意,
Jochen

Regards, Jochen

推荐答案

是的,有一种方法可以从后台线程保存管理对象上下文,或者更准确地说,它通常被称为在后台线程中,并显示在主线程中。这样,被管理对象在导入时被一个接一个地保存,而不是在终止时一次全部保存。

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.

我刚写了一个简短的回答一个类似的问题这里 SO ,但您应该阅读此 Apple doc
有很多潜在的陷阱,所以非常,非常仔细地阅读。然后阅读 Apple的有效导入数据 。这也是一个必读!而Marcus Zarra的 CoreData图书也很有用。

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多线程有点棘手,但它真的值得。祝你好运!

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

这篇关于核心数据的后台保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆