【问题标题】:redundant code in core data initialising stack核心数据初始化堆栈中的冗余代码
【发布时间】:2016-01-15 04:09:55
【问题描述】:

当我看到“初始化核心数据堆栈代码”时,我正在从苹果指南学习核心数据

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    NSError *error = nil;
    NSPersistentStoreCoordinator *psc = [[self managedObjectContext] persistentStoreCoordinator]

我不明白为什么你需要在第 37 行再次获取指向 psc 的指针,而它已经在第 27 行完成。块作用域不是一直到它所嵌入的函数吗?

文档链接:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html#//apple_ref/doc/uid/TP40001075-CH4-SW1

【问题讨论】:

    标签: ios objective-c core-data


    【解决方案1】:

    在块捕获状态的时间和块被执行的时间之间,需要操作的mocpsc可能已经改变。

    通过调用[self managedObjectContext],这种竞争条件大部分被消除了。 大部分只是因为它是一个并发队列,因此,其他一些队列可能会同时与 mocpsc 混在一起。

    即代码在一定程度上针对并发问题进行了强化。

    【讨论】:

      猜你喜欢
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多