CoreData 深入理解2 (iOS5 以后线程安全与同步)
2014-03-24 14:28
281 查看
up vote6down votefavorite 9 | I've seen a few videos / threads that say it's possible to create 'children' MOCs -- MOCs that use other MOCs as their persistant stores. Useful, for example, in a context where you're threading your application, and want to have a single master MOC that can save / rollback the changes that the child threads create. (From what I understand, a MOC and it's managedObjects MUST all be used on the same thread) The question is, how do I create a child MOC? I can't track down the WWDC videos I was watching that introduced them, and everything I"ve seen has been talking about how to use them ONCE they're made. I can easily alloc a new MOC, but how do I set it's persistent store to be another MOC? The reference doesn't show any functions that do that! objective-c ios xcode core-data nsmanagedobjectcontext
| |||
add comment |
2 Answers
activeoldestvotesup vote16down voteaccepted | Create a new MOC for which you are in total control of the synchronization. This is the same as calling initand the same behavior as pre-parent/child relationships. NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType]; You parent a MOC to another MOC by setting its property: moc.parentContext = someOtherMocThatIsNowMyParent; Here, the child chooses the parent. I'm sure my kids wish they were NSManagedObjectContexts. A parent context must be of either NSPrivateQueueConcurrencyTypeor NSMainQueueConcurrencyType. You can create a MOC that is "bound" to a private queue: NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; which means you should only access it via the performBlockor performBlockAndWaitAPI. You can call those methods from any thread as they will ensure proper serialization of the code in the block. For example... NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; The difference between performBlockand performBlockAndWaitis that performBlockwill create a block of code, and schedule it with Grand Central Dispatch to be executed asynchronously at some time in the future, on some unknown thread. The method call will return immediately. performBlockAndWaitwill do some magic synchronization with all the other performBlockcalls, and when all the blocks that have been presented prior to this one are done, this block will execute. The calling thread will pend until this call has completed. You can also create a MOC that is "bound" to the main thread, just like private concurrency. NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; moc.parentContext = someOtherMocThatIsNowMyParent; [moc performBlock:^{ // All code running in this block will be automatically serialized // with respect to all other performBlock or performBlockAndWait // calls for this same MOC. Furthermore, it will be serialized with // respect to the main thread as well, so this code will run in the // main thread -- which is important if you want to do UI work or other // stuff that requires the main thread. }]; which means you should only access it directly if you know you are on the main thread, or via the performBlockor performBlockAndWaitAPI. Now, you can use the "main concurrency" MOC either via the performBlockmethods, or directly if you know you are already running in the main thread.
| ||||||||||||
comment |
up vote1down vote | Initialize child MOC then:[_childMOC performBlockAndWait:^{ [_childMOC setParentContext:parentMOC]; }];
| ||||||||
|
相关文章推荐
- CoreData 深入理解2 (iOS5 以后线程安全与同步)
- coreData 深入理解3 (iOS5 以前线程安全与同步)
- coreData 深入理解3 (iOS5 以前线程安全与同步)
- coreData 深入理解4 --总结 (线程安全与同步--iOS5 前后对比)
- coreData 深入理解4 --总结 (线程安全与同步--iOS5 前后对比)
- coreData 深入理解4 --总结 (线程安全与同步--iOS5 前后对比)
- 深入理解线程安全和可重入函数
- 深入理解Servlet线程安全问题
- 深入理解同步/异步与阻塞/非阻塞区别
- 深入理解可重入与线程安全
- 深入理解非阻塞同步IO和非阻塞异步IO
- 对I2C总线的时钟同步和总线仲裁的深入理解
- 深入理解Servlet线程安全问题
- 深入理解Java并发3——线程安全与锁优化
- 深入理解并发/并行,阻塞/非阻塞,同步/异步
- iOS开发:深入理解GCD 第二篇(dispatch_group、dispatch_barrier、基于线程安全的多读单写)
- 深入理解非阻塞同步IO和非阻塞异步IO
- 深入理解并发/并行,阻塞/非阻塞,同步/异步
- 深入理解同步/异步与阻塞/非阻塞区别 (转)
- 深入理解计算机系统——第12章:用信号量同步线程