-[performBlock:]
and -[performBlockAndWait:]
are used to your app avoid accessing a managed object context or managed object from the wrong dispatch queue.
Let's imagine I have set up the managed object context as a background context with PrivateQueueConcurrencyType and we run this code on the main thread:
NSManagedObject *mo = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:backgroundContext];
mo.street = "Rue la place"
[backgroundManagedObjectContext save:&error]
This piece of code violates Core Data’s concurrency model by calling -[insertNewObjectForEntityForName::]
with the private queue context as an argument from the main thread.
So we should wrap all access to backgroundContext in a block passed to performBlock: or performBlockAndWait:, which executes the block on the context’s private dispatch queue:
[self.backgroundManagedObjectContext performBlockAndWait:^{
NSManagedObject *mo = [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:backgroundContext];
mo.street = "Rue la place"
[backgroundManagedObjectContext save:&error]
}];