NSOperationQueue指定线程 [英] NSOperationQueue designated thread

查看:67
本文介绍了NSOperationQueue指定线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 NSOperationQueue 调度CoreData操作。但是,操作队列行为并不总是相同的(例如,它在使用线程池的iOS 4.0 / OS 10.6上使用 libdispatch 进行分派),并且队列可能并不总是使用同一线程(按照 NSManagedObjectContext 的要求。)

I want to use an NSOperationQueue to dispatch CoreData operations. However, operation queue behavior is not always the same (e.g. it dispatches using libdispatch on iOS 4.0/OS 10.6 which uses thread pools) and a queue might not always use the same thread (as NSManagedObjectContext requires).

我可以强制执行串行 NSOperationQueue 在单个线程上执行?
还是我必须为此创建自己的简单排队机制?

Can I force a serial NSOperationQueue to execute on a single thread? Or do I have to create my own simple queuing mechanism for that?

推荐答案


我可以强制在一个线程上执行一个串行NSOperationQueue吗?
还是我必须为此创建自己的简单排队机制?

Can I force a serial NSOperationQueue to execute on a single thread? Or do I have to create my own simple queuing mechanism for that?

您不需要执行任何一项操作那些。 Core Data真正需要的是您没有两个代码同时更改托管对象上下文。在与核心数据并发

You shouldn't need to do either of those. What Core Data really requires is that you don't have two pieces of code making changes to a managed object context at the same time. There's even a note on this at the very beginning of Concurrency with Core Data:


注意:您可以使用线程,串行操作队列,或调度并发队列。
为了简洁起见,本文通篇使用线程来指代其中任何一个。

Note: You can use threads, serial operation queues, or dispatch queues for concurrency. For the sake of conciseness, this article uses "thread" throughout to refer to any of these.

真正需要什么是您在给定的上下文中序列化操作。如果使用单个线程,则自然会发生这种情况,但是如果将 maxConcurrentOperationCount 设置为1,NSOperationQueue也会序列化其操作,因此您不必担心确保所有操作都采用放在同一线程上。

What's really required is that you serialize operations on a given context. That happens naturally if you use a single thread, but NSOperationQueue also serializes its operations if you set maxConcurrentOperationCount to 1, so you don't have to worry about ensuring that all operations take place on the same thread.

这篇关于NSOperationQueue指定线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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