What thread is blocking running on?

A simple question: my main UIManagedDocument data file is created in the main thread, what do I want to know when I call executeBlock, it blocks the queue / starts the main thread (the same thread where the managed document was created)?

dispatch_queue_t backgroundQueue = dispatch_queue_create("DownloadQueue", NULL); dispatch_async(backgroundQueue, ^{ // Get data from web [document.managedObjectContext performBlock:^{ // Add data to Core Data }]; }); 
+4
source share
2 answers

Using UIManagedDocument creates a managedObjectContext (as with OS X v10.7 and iOS 5), using the default Confinement standard NSConfinementConcurrencyType . Simply put, this type of concurrency indicates that the context will not be used in any other thread than the one that originally created the context.

In this case, a UIManagedDocument (and its context) were created in the main thread, the performBlock: will execute the linked block back to the main thread. Using performBlock: you really don't need to know in which thread the managed Object ObjectContext is created, messaging performBlock: will always execute the block associated with it in the same thread in which the context is located.

0
source

You can read apple release notes: CoreData release notes for iOS5

You can use contexts using the restriction pattern, just like you did before OS X v10.7 and iOS 5. You send context messages directly; so that you send messages with the right queue.

You use contexts using queue-based concurrency types in conjunction with two new methods: performBlock: and performBlockAndWait :. You group the β€œstandard” messages to be sent to the context (including initialization, such as setting up a permanent repository coordinator, etc.) inside the block to go to one of these methods. The one exception: if your code is executing in the main thread, you can directly refer to methods in the contexts of the main queue style instead of using block-based APIs.

execute Block: and execute BlockAndWait: ensure the execution of block operations performed in the queue specified for the context. Execution: the method returns immediately, and the context executes the block methods on its own topic. Using the performBlockAndWait: method, the context still executes the block methods in its thread, but the method does not return until the block is executed.

It is important to understand that the blocks are executed as a separate work plan. As soon as your block ends, someone else can queue another block, discard the changes, reset the context, etc. Thus, the blocks can be quite large and usually ends with a save: call.

+2
source

Source: https://habr.com/ru/post/1437883/


All Articles