iOS5 NSManagedObjectContext并发类型及其使用方式? [英] iOS5 NSManagedObjectContext Concurrency types and how are they used?

查看:143
本文介绍了iOS5 NSManagedObjectContext并发类型及其使用方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前关于新的NSManagedObjectContext并发类型的文献似乎有点稀疏。除了WWDC 2011 vids以及我在此过程中提到的其他一些信息之外,我仍然很难掌握每种并发类型的使用方式。以下是我如何解释每种类型。如果我理解错误,请纠正我。

Literature seems a bit sparse at the moment about the new NSManagedObjectContext concurrency types. Aside from the WWDC 2011 vids and some other info I picked up along the way, I'm still having a hard time grasping how each concurrency type is used. Below is how I'm interpreting each type. Please correct me if I'm understanding anything incorrectly.

这种类型在过去几年中一直是常态。 MOC被屏蔽了每个线程。因此,如果线程A MOC想要通过保存消息合并来自线程B MOC的数据,则线程A将需要订阅线程B的MOC保存通知。

This type has been the norm over the last few years. MOC's are shielded from each thread. So if thread A MOC wants to merge data from Thread B MOC via a save message, thread A would need to subscribe to thread B's MOC save notification.

每个MOC树(父级和子级MOC)共享相同的队列,无论每个线程是什么线程是的。因此,每当发送来自任何这些上下文的保存消息时,它将被放入专门为此MOC树提供的私有提示。

Each MOC tree (parent & children MOCs) share the same queue no matter what thread each is on. So whenever a save message from any of these contexts is sent, it is put in a private cue specifically made only for this MOC tree.

仍然对这个感到困惑。从我收集的内容就像NSPrivateQueueConcurrencyType一样,只有私有队列在线程上运行。我读到这对于与MOC的UI通信是有益的,但为什么呢?为什么我会选择NSPrivateQueueConcurrencyType呢?我假设因为NSMainQueueConcurrencyType在主线程中执行,这是否允许后台进程?这不是不使用线程吗?

Still confused by this one. From what I gather it's the like NSPrivateQueueConcurrencyType, only the private queue is run on the main thread. I read that this is beneficial for UI communications with the MOC, but why? Why would I choose this over NSPrivateQueueConcurrencyType? I'm assuming that since the NSMainQueueConcurrencyType is executed in the main thread, does this not allow for background processes? Isn't this the same as not using threads?

推荐答案

队列并发类型可以帮助您管理多线程核心数据:

The queue concurrency types help you to manage mutlithreaded core data:

对于这两种类型,当您使用其中一种performBlock方法执行操作时,操作仅发生在正确的队列中。即。

For both types, the actions only happen on the correct queue when you do them using one of the performBlock methods. i.e.

[context performBlock:^{
    dataObject.title = @"Title";
    [context save:nil]; // Do actual error handling here
}];

私有队列并发类型在后台线程中完成所有工作。非常适合处理或磁盘io。

The private queue concurrency type does all it's work in a background thread. Great for processing or disk io.

主队列类型只是在UIThread上执行所有操作。这是必要的,当你需要
做一些事情,比如绑定一个NSFetchedResultsController,或任何其他需要与处理该上下文对象交织的ui相关任务。

The main queue type just does all it's actions on a UIThread. That's neccesary for when you need to do things like bind a NSFetchedResultsController up to it, or any other ui related tasks that need to be interwoven with processing that context's objects.

当你将它们组合起来时真正的乐趣。想象一下,有一个父上下文在一个私有队列上下文的后台线程上执行所有操作,然后您可以对主队列类型的子上下文执行所有操作。这基本上是UIManagedDocument所做的。它可以让您保持UI队列免于必须完成的繁忙工作来管理数据。

The real fun comes when you combine them. Imagine having a parent context that does all io on a background thread that is a private queue context, and then you do all your ui work against a child context of the main queue type. Thats essentially what UIManagedDocument does. It lets you keep you UI queue free from the busywork that has to be done to manage data.

这篇关于iOS5 NSManagedObjectContext并发类型及其使用方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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