In iOS 5, NSManagedObjectContext has a couple of new methods, performBlock: and performBlockAndWait:. What are these methods actually used for? What do they replace in older versions? What kind of blocks are supposed to be passed to them? How do I decide which to use? If anyone has some examples of their use it would be great.
In iOS 5, NSManagedObjectContext has a couple of new methods, performBlock: and performBlockAndWait: .
Share
The methods
performBlock:andperformBlockAndWait:are used to send messages to yourNSManagedObjectContextinstance if the MOC was initialized usingNSPrivateQueueConcurrencyTypeorNSMainQueueConcurrencyType. If you do anything with one of these context types, such as setting the persistent store or saving changes, you do it in a block.performBlock:will add the block to the backing queue and schedule it to run on its own thread. The block will return immediately. You might use this for long persist operations to the backing store.performBlockAndWait:will also add the block to the backing queue and schedule it to run on its own thread. However, the block will not return until the block is finished executing. If you can’t move on until you know whether the operation was successful, then this is your choice.For example:
Note that because I did a
performBlockAndWait:, I can access the error outside the block.performBlock:would require a different approach.From the iOS 5 core data release notes: