iPhone – Core Data Concurrent (NSOpe)

wrote in Apple docs:

…you should create the context in
main ( for a serial queue) or start
(for a concurrent queue).

But I really don’t get any difference. Why can’t I create a context for the concurrent queue in main? I tried it and it works exactly the same way I did at the beginning.

There is one more thing that confused me. From the documentation of the start method:

p>

…If you are implementing a
concurrent operation, you must
override this method and use it to
initiate your operation.

So, why can’t I initialize everything in main (or maybe it shouldn’t)?

There may be differences between what you call “context” and “concurrency,” and Apple’s term The meaning is also different. They use “concurrency” in a specific (and confusing) way. Starting with Managing Concurrency With NSOperation, this section may help:

There are a number of different ways that you can use NSOperation, but the most common is to write a custom subclass and override one method: main. The main method gets called to perform the operation when the NSOperationQueue schedules it to run . NSOperation classes written in this way are known as non-concurrent operations, because the developer is not responsible for spawning threads—multi-threading is all handled by the super class. (Don’t be confused by the terminology: just because an operation is non-concurrent, does not mean it cannot be executed concurrently, it simply means that you don’t have to handle the concurrency yourself.)

If you need more control over threading and the run-time environment of your operations, you can make use of concurrent opera tions. To do this, you subclass NSOperation and override the start method. In the start method, you can spawn threads and setup the environment before calling the main method. You are also required to maintain the state of the NSOperation by setting properties like isExecuting and isFinished. In short, concurrent operations give you a lot more control, but also demand more effort—for most tasks non-concurrent operations suffice.

Apple docs wrote:

…you should create the context in
main (for a serial queue) or start
(for a concurrent queue).

But I really don’t get any difference. Why can’t I create a context for the concurrent queue in main? I tried it and it works exactly the same way I did at the beginning.

There is one more thing that confused me. From the documentation of the start method:

p>

…If you are implementing a
concurrent operation, you must
override this method and use it to
initiate your operation.

So, why can’t I initialize everything in main (or maybe it shouldn’t)?

There may be differences between what you call “context” and “concurrency,” and Apple’s terms have different meanings. They are specific (and confusing ) To use “concurrency”. Starting from Managing Concurrency With NSOperation, this section may be helpful:

There are a number of different ways that you can use NSOperation, but the most common is to write a custom subclass and override one method: main. The main method gets called to perform the operation when the NSOperationQueue schedules it to run. NSOperation classes written in this way are known as non- concurrent operations, because the developer is not responsible for spawning threads—multi-threading is all handled by the super class. (Don’t be confused by the terminology: just because an operation is non-concurrent, does not mean it cannot be executed concurrently, it simply means that you don’t have to handle the concurrency yourself.)

If you need more control over threading and the run-time environment of your operations, you can make use of concurrent operations. To do this, you subclass NSOperation and override the start method. In the start method, you can spawn threads and setup the environment before calling the main method. You are also required to maintain the state of the NSOperation by setting properties like isExecuting and isFinished. In short, concurrent operations give you a lot more control, but also demand more effort—for most tasks non-concurrent operations suffice.

Leave a Comment

Your email address will not be published.