Creating Background Thread for Core Data writing(为核心数据写入创建后台线程)
问题描述
我正在尝试重新创建这篇 cocoanetics 文章中描述的三层核心数据系统 (http://www.cocoanetics.com/2012/07/multi-context-coredata/).我遇到的问题是在自己的后台线程上创建私有 MOC.我对多线程以及如何在目标 c 中完成它没有太多经验.我一直在阅读文章并试图掌握如何正确实施这种方法,但我最终承认我不知道自己在做什么.
I'm trying to recreate the three tier core data system that is described in this cocoanetics article ( http://www.cocoanetics.com/2012/07/multi-context-coredata/). The problem I'm running into is creating the private MOC on its own background thread. I don't have much experience with multithreading and how it should be done in objective c. I've been reading over articles and trying to grasp how to correctly implement this approach, but I have finally conceded to the fact that I don't know what I'm doing.
要创建这种方法,我是否需要创建一个 NSThread 并对其进行管理?还是有我不理解的更简单的方法?
To create this approach, do I need to create an NSThread and manage it? Or is there a simpler way that I'm not understanding?
推荐答案
在后台执行 Core Data 操作的最简单方法是创建 NSPrivateQueueConcurrencyType
的托管对象上下文.这种类型的 MOC 创建和管理私有队列.使用 performBlock
或 performBlockAndWait
在私有 MOC 上执行操作可确保使用正确的队列.
The easiest way to perform Core Data operations in the background is to create a managed object context of the NSPrivateQueueConcurrencyType
. This type of MOC creates and manages a private queue. Using performBlock
or performBlockAndWait
to execute operations on the private MOC ensures that the right queue is used.
参见 托管对象上下文的并发支持在 OS X v10.7 和 iOS 5.0 的核心数据发行说明中了解详细信息和示例.
See Concurrency Support for Managed Object Contexts in the Core Data Release Notes for OS X v10.7 and iOS 5.0 for details and examples.
我只能推荐观看 WWDC 2011 Session 303What's New in Core Data on iOS"的视频或幻灯片,其中解释了 Core Data 并发.
I can only recommend to watch the video or slides from the WWDC 2011 Session 303 "What’s New in Core Data on iOS", where Core Data concurrency is explained.
这篇关于为核心数据写入创建后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为核心数据写入创建后台线程
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01