问题描述
我有一个具有表视图的导航视图,当一行被点击时,行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:
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数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!