问题描述
我想使用 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 $ c吗? $ c>在单个线程上执行?
还是我必须为此创建自己的简单排队机制?
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:
真正需要什么是您在给定的上下文中序列化操作。如果使用单个线程,则自然会发生这种情况,但是如果将 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指定线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!