【发布时间】:2016-01-02 13:39:00
【问题描述】:
我的 iOS 8.0 + 应用程序本质上是一个字典应用程序,以索引的、易于导航的格式向用户呈现只读数据集。我已经探索了几种加载静态数据的策略,并且我决定在应用程序首次打开时将几个JSON 数据文件序列化并加载到Core Data 存储中。因此,对managedObjectContext.save() 的调用将在应用的生命周期内仅在首次使用时发生一次。
通过阅读 Mac 开发人员库(2015 年 9 月更新)中 Apple 的 Core Data Programming Guide,我了解到Apple 的 建议做法是
1) 将 Core Data 堆栈与 AppDelegate 分离成一个 dedicated DataController object (这使得即使在 Xcode 7.2 中仍然放置 Core Data 堆栈看起来很奇怪默认情况下在AppDelegate 中,但无论如何...);和
2) 使用dispatch_async 块在background thread 中打开(并且,我假设是种子/加载)持久存储,如下所示:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
//(get URL to persistent store here)
do {
try psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil)
//presumably load the store from serialized JSON files here?
} catch { fatalError("Error migrating store: \(error)") }
}
我刚刚开始学习并发和GCD,所以我的问题很基础:
1) 如果数据集正在后台线程中加载,这可能需要一些不小的时间才能完成,初始view controller 如何知道数据何时完成加载,以便它可以从ManagedObjectContext 显示在 UITableView 中?
2) 与此类似,如果我想通过运行一些提取并将调试文本打印到控制台来测试完全加载的数据集,我如何知道后台进程何时完成并且可以安全地开始查询?
谢谢!
p.s.我正在 swift 开发,所以任何关于 swift 的提示都会非常棒。
【问题讨论】:
-
@Hiren 请不要对仅将文字加粗的帖子进行琐碎的编辑,或对帖子本身的内容进行最小的更改。
标签: ios multithreading swift core-data