CoreData 深入理解2 (iOS5 以后线程安全与同步)

翻译 2014年03月24日 14:28:51

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!

share|improve this question
  add comment

2 Answers

up vote16down voteaccepted

Create a new MOC for which you are in total control of the synchronization. This is the same as calling init and 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 NSPrivateQueueConcurrencyType or 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 performBlock or performBlockAndWait API. 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];
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.
    // Access "moc" to your heart's content inside these blocks of code.
}];

The difference between performBlock and performBlockAndWait is that performBlock will 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.

performBlockAndWait will do some magic synchronization with all the other performBlock calls, 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 performBlock or performBlockAndWait API.

Now, you can use the "main concurrency" MOC either via the performBlock methods, or directly if you know you are already running in the main thread.

share|improve this answer
 
 
Thank you for such an (incredibly) thorough answer. –  RonLugge Sep 4 '12 at 22:15
 
Hello, I know it's really old but my question isn't big enough to create a new one, I was wondering where I should call save on the context, inside the performBlock or outside ? Thank you ! –  ItsASecret Oct 12 '13 at 16:15
 
Inside performBlock –  Jody Hagins Oct 16 '13 at 1:57
add comment

Initialize child MOC then:

[_childMOC performBlockAndWait:^{
            [_childMOC setParentContext:parentMOC]; 
 }];
share|improve this answer
 
 
So... I can ACCESS MOCs accross threads, I'm just not allowed to operate (insert, delete, save, rollback) accross them? –  RonLugge Sep 4 '12 at 21:46
1  
You can call performBlock and performBlockAndWait from other threads. Not much else. –  Jody Hagins Sep 4 '12 at 22:11 

coreData 深入理解4 --总结 (线程安全与同步--iOS5 前后对比)

目录(?)[+] Core Data是iOS中很重要的一个部分,可以理解为基于SQLite(当然也可以是其他的Storage,如In-memory,只是SQLite比较常见)的一个ORM实现...

coreData 深入理解4 --总结 (线程安全与同步--iOS5 前后对比)

Core Data是iOS中很重要的一个部分,可以理解为基于SQLite(当然也可以是其他的Storage,如In-memory,只是SQLite比较常见)的一个ORM实现,所以有关系数据库的特性,又...

IOS网络笔记--多线程编程2(线程安全--同步锁)

申明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨  /*      情景:哆啦A梦、大雄、胖虎三人各自在自己的手机上买火车票想去富士山旅游,哆啦A梦想和它妹妹一起去,想买2张票;大雄想和静香...

iOS开发:深入理解GCD 第二篇(dispatch_group、dispatch_barrier、基于线程安全的多读单写)

Dispatch Group 在追加到Dispatch Queue中的多个任务处理完毕之后想执行结束处理,这种需求会经常出现。如果只是使用一个Serial Dispatch Queue(串行队列)时...

iOS开发:深入理解GCD 第二篇(dispatch_group、dispatch_barrier、基于线程安全的多读单写)

Dispatch Group 在追加到Dispatch Queue中的多个任务处理完毕之后想执行结束处理,这种需求会经常出现。如果只是使用一个Serial Dispatch Queue(串行队列)时...

java线程线程安全同步线程

  • 2010年11月30日 08:54
  • 405KB
  • 下载

java线程安全与同步

  • 2013年03月06日 23:01
  • 84KB
  • 下载

CoreData线程安全

CoreData中的NSManagedObjectContext在多线程中不安全,如果想要多线程访问CoreData的话,最好的方法是一个线程一个NSManagedObjectContext, ,每...

关于CoreData和SQLite多线程访问时的线程安全问题

关于CoreData和SQLite多线程访问时的线程安全问题数据库读取操作一般都是多线程访问的。在对数据进行读取时,我们要保证其当前状态不能被修改,即读取时加锁,否则就会出现数据错误混乱。 IOS中...

关于CoreData和SQLite多线程访问时的线程安全问题

关于CoreData和SQLite多线程访问时的线程安全问题 数据库读取操作一般都是多线程访问的。在对数据进行读取时,我们要保证其当前状态不能被修改,即读取时加锁,否则就会出现数据错误混乱。  ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CoreData 深入理解2 (iOS5 以后线程安全与同步)
举报原因:
原因补充:

(最多只允许输入30个字)