ios - Coredata 跨线程访问新发现?
大家讲道理
大家讲道理 2017-04-17 17:58:30
0
2
739

最近读了Coredata第一三方库Magical Record作者的一篇文章链接描述,对Coredata访问有了新的疑惑,performBlockAPI 是如何保证线程安全的?

众所周知 , NSManagedObjectContext(简称moc) 是不允许跨线程访问的 ,用人话说就是 在哪个线程创建的moc ,就在哪个线程使用它.

苹果在iOS5.0之后 推出了一个新的 api 操作moc. 有代码长这样 :

__block BOOL savedOK = NO;
[myMOC performBlock:^{
    // Do lots of things with the context.
    savedOK = [myMOC save:&error];
}];

即是通过NSPrivateQueueConcurrencyType类型创建的MOC 系统会为其分配一个私有队列,而不需要我们手动为它创建线程, 从此并发变的更容易 。

但是通过上面那篇文章, Magical Record 作者指出, 苹果的新api 使用了GCD , 而在GCD中的并发我们是通过队列来实现的,即我们只关注和操作队列,而不是直接操作线程。 当你提交一个block代码块到GCD的队列上,队列会自动分配线程去执行这个block。 但是,GCD不能保证这个队列始终在一条线程运行!! 也就是说,当你提交block到队列时,是将任务放到线程A执行,但下一刻,也许任务已经不在线程A而在线程B了!虽然此时依旧在同一个队列!这即是作者弃用contextForCurrentThread
的原因。

那么问题来了,如上文代码中,myMOC是一个NSPrivateQueueConcurrencyType类型的上下文,创建时系统为其分配了私有队列Q,我们假设myMOC创建发生在线程A,线程A属于队列Q,但由于GCD的特性,某一刻将block任务分配到了属于队列Q的线程B ,也就是说,此时,创建于线程A的myMOC 可能会在线程B被访问 , 这就发生了跨线程访问!

这又如何解释呢? 希望对这部分了解的大虾给予解答,如果我解决了这个问题,也会在楼下贴出 。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
Ty80

Thank you very much for your answer. But there seem to be two more questions:

1. The author’s article did not directly say “Apple’s new API uses GCD”. Where is the sentence However, now, as more of you (and the heart of MagicalRecord) is using GCD, 是说MagicalRecord的核心是用了GCD,可我翻遍MagicalRecord的源码,似乎并没有发现使用GCD调度队列来实现异步操作,核心都是用了performBlockperformBlockAndWait来实现对MOC的异步操作.所以我猜测作者的意思是performBlock会与GCD有关,不然为何 明明没有用到GCD,却因为GCD的特性而弃用之前的接口 contextForCurrentThread in the original article?

2. Put aside question 1, part of Apple’s documentation says this:

The NSPrivateQueueConcurrencyType configuration creates its own queue upon initialization and can be used only on that queue. Because the queue is private and internal to the NSManagedObjectContext instance, it can only be accessed through the performBlock: and the performBlockAndWait: methods.

NSPrivateQueueConcurrencyType type MOC will create its own queue during initialization. This queue is private within this MOC. So can it be understood as:
I created a content under NSPrivateQueueConcurrencyType的MOC,它会创建一个私有队列,在线程b中去管理performBlock in thread a.

若有代码:

  NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [moc performBlock:^{
        // ... do things
        NSError *error;
        [moc save:&error];
    }];
    

If the context is created in thread a, it should belong to thread a, but it is accessed in thread b of the queue. Isn’t it a cross-thread access?

Does

conflict with performBlock which guarantees execution in the thread to which the current context belongs? How is it guaranteed? Is b and a the same thread when creating a private queue? How to do it?

PHPzhong

I read the article you linked to, "But through the above article, the author of Magical Record pointed out that Apple's new api uses GCD, and the concurrency in GCD is implemented through queues." There is no such thing. I saw the description in your sentence above that "Apple's new API uses GCD".

The following is the referenced Apple document. The performBlock method will ensure that it is executed in the thread to which the current context belongs:

performBlock: and performBlockAndWait: ensure the block operations are executed on the queue specified for the context. The performBlock: method returns immediately and the context executes the block methods on its own thread. With the performBlockAndWait: method, the context still executes the block methods on its own thread, but the method doesn’t return until the block is executed.
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template