为核心数据写入创建后台线程

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 创建和管理私有队列.使用 performBlockperformBlockAndWait 在私有 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.

这篇关于为核心数据写入创建后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为核心数据写入创建后台线程