Aaron
Aaron

Reputation: 619

Is [NSManagedObjectContext performBlock] synchronous if run on the context's thread?

Say I have a managed object context tied to the main thread, and I call [mainContext performBlock:block] on the main thread. Will that run synchronously or is it still scheduled and run at a later time?

Upvotes: 2

Views: 700

Answers (1)

omz
omz

Reputation: 53551

Assuming you mean NSMainQueueConcurrencyType (and not NSConfinementConcurrencyType), calling performBlock: behaves like dispatch_async, i.e. your block will be enqueued and not be executed immediately.

You can verify this easily:

NSLog(@"before block");
[self.managedObjectContext performBlock:^{
    NSLog(@"in block");
}];
NSLog(@"after block");

This will print (in that order):

before block
after block
in block

If you need to perform a block synchronously, you have to use performBlockAndWait: for both queue-based concurrency types.

Upvotes: 3

Related Questions