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.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- Popover segue to static cell UITableView causes co
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
The methods
performBlock:
andperformBlockAndWait:
are used to send messages to yourNSManagedObjectContext
instance if the MOC was initialized usingNSPrivateQueueConcurrencyType
orNSMainQueueConcurrencyType
. 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:
They allow you to access the same
managedObjectContext
accross threads.I am not really sure I am correct, but this is how I use it.
You use
performBlockAndWait
is like "usual". You do not need it if you execute the managedObjectContext only on one thread. If you execute it on many threads then yes you will needperformBlock
.So, if you're on main thread, you do not need to do
performBlockAndWait
for the mainmanagedObjectContext
. At least I don't and is doing fine.However if you access that
managedObjectContext
on other threads then yes you will need to doperformBlockAndWait
.So that's the purpose of
performBlock
andperformBlockAndWait
.Would someone please correct me if I am wrong here. Of course if you access the context only on one thread then you can simply use the default.