在后台获取Core数据 [英] Fetching Core Data in the background

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

问题描述

我有一个具有表视图的导航视图,当一行被点击时,行indexPath被传递给下一个视图。

I have a Navigation View with a Table View, when a row is clicked, the row indexPath is passed to the next view.

在Details视图viewDidLoad,我从Core Data中获取数据。我使用从应用程序委托提取

in the Details view viewDidLoad, i am fetching data from Core Data. i use the fetching from the application delegate

[appDelegate loadItem:i];

正如你可以看到,我只传递一个整数,它携带行号。

As you can see i am passing one integer only which carries the row number.

问题是:
如何(在后台)
我需要这个,因为有时获取的结果太大,所以处理需要3秒延迟推送细节视图。

the question is: How can i make this call process in another thread.(in the background) I need this because sometimes the result of the fetch is too big, so the processing takes 3 seconds delaying pushing the Details View.

我需要显示一个活动指示符三秒钟,所以我需要把Fetch在另一个线程能够使用UI的指标,同时抓取正在处理。

I need to display an Activity Indicator for the three seconds, so i need to put the Fetch in another thread to be able to use the UI for the indicator while the fetch is being processed.

我想要最简单的方式,因为我是一个新手。发布一些代码将是伟大的。
或links:)

I want the simplest way as i am a newbie. Posting some codes will be great. or links :)

推荐答案

混合多线程和Core Data不是一个简单的任务。 核心数据编程指南的多线程部分介绍了如何在多个线程上与Core Data进行交互,包括所有需要注意的事项。

Mixing multithreading and Core Data is not a simple task. The "Multi-Threading with Core Data" section of the Core Data Programming Guide describes how to interact with Core Data on multiple threads, including all the things that you need to be careful of.

基本上,你将需要为每个线程创建一个单独的受管对象上下文。这些上下文可以共享对一个受管对象模型和持久存储的访问。对于您的情况,他们建议如下:

Basically, you will need to create a separate managed object context for each thread. These contexts can share access to one managed object model and persistent store. For your case, they suggest the following:


您使用两个管理对象上下文
与单个持久性
相关联商店协调员。您在后台
线程上获取一个
受管对象上下文,并将
已获取对象的对象ID传递到另一个线程。在
的第二个线程(通常是
应用程序的主线程,所以你
然后可以显示结果),你使用
第二个上下文在对象中的故障
(你使用
objectWithID:来实例化
对象)。

You use two managed object contexts associated with a single persistent store coordinator. You fetch in one managed object context on a background thread, and pass the object IDs of the fetched objects to another thread. In the second thread (typically the application's main thread, so that you can then display the results), you use the second context to fault in objects with those object IDs (you use objectWithID: to instantiate the object).

BackgroundFetching示例应用程序显示了如何做到这一点,但我的系统上没有这个。

It sounds like the BackgroundFetching sample application shows how to do this, but I don't have it on my system.

但是,在你对你的获取请求太多的多线程之前, d仔细看看为什么它需要这么长的时间加载。我首先建议使用-setFetchBatchSize:在你的NSFetchRequest来限制通过你的fetch(这将节省你大量的内存)加载到内存的对象的数量。接下来,我将使用-setPropertiesToFetch:将获取的属性限制为只有那些您将立即使用的属性。

However, before you get too far into multithreading your fetch request, I'd take a hard look into why it's taking so long to load. I'd first recommend using -setFetchBatchSize: on your NSFetchRequest to limit the number of objects loaded into memory via your fetch (which will save you a lot of memory, too). Next, I'd use -setPropertiesToFetch: to limit the properties fetched to only those you'll be using immediately.

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

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