核心数据和线程/Grand Central Dispatch
我是 Grand Central Dispatch (GCD) 和 Core Data 的初学者,我需要您的帮助才能将 Core Data 与 CGD 结合使用,以便在我向 Core Data 添加 40.000 条记录时 UI 不会被锁定。
我知道 CD 不是线程安全的,所以我必须使用另一个上下文,然后保存数据并合并上下文,据我从一些文章中了解到的。
我还不能做的就是将各个部分组合在一起。
因此,在我的代码中,我需要您的帮助来了解如何做到这一点。
我有:
/*some other code*/
for (NSDictionary *memberData in arrayWithResult) {
//get the Activities for this member
NSArray *arrayWithMemberActivities = [activitiesDict objectForKey:[memberData objectForKey:@"MemberID"]];
//create the Member, with the NSSet of Activities
[Members createMemberWithDataFromServer:memberData
andActivitiesArray:arrayWithMemberActivities
andStaffArray:nil
andContactsArray:nil
inManagedObjectContext:self.managedObjectContext];
}
如何将其转换为在后台工作,然后在保存完成后保存数据并更新 UI,而不会在保存 40.000 个对象时阻塞 UI?
I'm a beginner with Grand Central Dispatch (GCD) and Core Data, and I need your help to use Core Data with CGD, so that the UI is not locked while I add 40.000 records to Core Data.
I know that CD is not thread safe, so I have to use another context, and then save the data and merge contexts, as far as I was able to understand from some articles.
What I couldn't do yet, is put the pieces together.
So, in my code, I need your help on how to to that.
I have:
/*some other code*/
for (NSDictionary *memberData in arrayWithResult) {
//get the Activities for this member
NSArray *arrayWithMemberActivities = [activitiesDict objectForKey:[memberData objectForKey:@"MemberID"]];
//create the Member, with the NSSet of Activities
[Members createMemberWithDataFromServer:memberData
andActivitiesArray:arrayWithMemberActivities
andStaffArray:nil
andContactsArray:nil
inManagedObjectContext:self.managedObjectContext];
}
How can I transform this to work on the background, and then, when done saving, save the data and update the UI, without blocking the UI while saving the 40.000 objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个很好的例子供您尝试。如果您有任何问题,请随时回来:
并响应上下文保存通知:
完成后台线程上下文后,不要忘记从通知中心删除观察者。
Here's a good example for you to try. Feel free to come back if you have any questions:
And in response to the context save notification:
And don't forget to remove the observer from the notification center once you are done with the background thread context.
下面是一个片段,用最简单的术语介绍了 GCD 和 UI。您可以将 doWork 替换为执行 CoreData 工作的代码。
关于 CD 和线程安全,GCD 的好处之一是您可以划分应用程序(子系统)的区域以同步并确保它们在同一队列上执行。您可以在名为 com.yourcompany.appname.dataaccess 的队列上执行所有 CoreData 工作。
在示例中,有一个调用长时间运行的工作的按钮、一个状态标签,我添加了一个滑块以显示我可以在背景工作完成时移动滑块。
Here's a snippet which covers GCD and UI in it's simplest terms. You can replace doWork with your code that does the CoreData work.
Concerning CD and thread safety, one of the nice parts about GCD is you can sections off areas of your application (subsystems) to synchronize and ensure they get executed on the same queue. You could execute all CoreData work on a queue named com.yourcompany.appname.dataaccess.
In the sample, there's a button which invokes the long running work, a status label, and I added a slider to show I can move the slider while the bg work is done.
这篇博文有关于Core Data并发的详细描述和示例代码:
http://www.duckrowing.com/ 2010/03/11/在多线程上使用核心数据/
This blog post has a detailed description on Core Data concurrency and sample code:
http://www.duckrowing.com/2010/03/11/using-core-data-on-multiple-threads/
添加另一个信息源,您可以检查
ThreadedCoreData
Apple iOS 开发者库的示例代码,最近已更新 (2013-06-09)
Adding another source of info you can check
ThreadedCoreData
the Sample Code of Apple's iOS Developer Library, which have been recently updated (2013-06-09)
因此,为此选择的答案是近两年前的,并且存在一些问题:
--编辑--
进一步研究 #3 - waitUntilDone:YES 不是托管上下文对象的有效 methodSignature,那么它是如何工作的呢?
So the selected answer for this is from nearly 2 years ago now, and there's a few issues with it:
--Edit--
Looking further into #3 - waitUntilDone:YES isn't a valid methodSignature for managed context objects, so how does that even work?
比将持久存储协调器附加到新上下文更简单的方法,这也不是线程安全的,顺便说一句。
关于如何使用多上下文核心数据的精彩教程:
http://www. cocoanetics.com/2012/07/multi-context-coredata/
Much easier way to do it than attach the persistent store coordinator to a new context, which is not thread safe either, btw.
Great tutorial on how to use multi-context Core Data:
http://www.cocoanetics.com/2012/07/multi-context-coredata/