[英]Having to call fetch twice from CoreData
Both on simulator and my real device, an array of strings is saved upon app termination. 在模拟器和我的真实设备上,在应用终止时都会保存一串字符串。 When I restart the app and fetchRequest for my persisted data (either from a viewDidLoad or a manual button action), I get an empty array on the first try.
当我重新启动应用程序并为我的持久数据fetchRequest(通过viewDidLoad或手动按钮操作)时,第一次尝试时得到一个空数组。 It isn't until the second time I fetchRequest that I finally get my data.
直到我第二次fetchRequest才终于得到我的数据。
The funny thing is that there doesn't seem to be a time discrepancy involved in this issue. 有趣的是,这个问题似乎没有时间上的差异。 I tried setting various timeouts before trying to fetch the second time.
在尝试第二次获取之前,我尝试设置各种超时。 It doesn't matter whether I wait 10 seconds to a minute -- or even immediately after the first fetch;
我等待10秒到一分钟,甚至是在第一次获取之后立即都没关系; the data is only fetched on the second try.
仅在第二次尝试时才获取数据。
I'm having to use this code to fetch my data: 我必须使用以下代码来获取数据:
var results = try self.context.fetch(fetchRequest) as! [NSManagedObject]
while (results.isEmpty) {
results = try self.context.fetch(fetchRequest) as! [NSManagedObject]
}
return results
For my sanity's sake, here's a checklist: 为了我的理智,这是一个清单:
private static let context: NSManagedObjectContext = DataController().managedObjectContext
private static let context: NSManagedObjectContext = DataController().managedObjectContext
顶部的静态变量private static let context: NSManagedObjectContext = DataController().managedObjectContext
Note** I forgot to mention that I'm building a framework. 注意**我忘了提到我正在构建一个框架。 I am using CoreData with the framework's bundle identifier and using the model contained in the framework, so I want to avoid having to use logic outside of the framework (other than initalizing the framework in the appDelegate).
我将CoreData与框架的包标识符一起使用,并使用框架中包含的模型,因此我想避免不得不在框架外部使用逻辑(除了在appDelegate中初始化框架)。
应该在位于appDelegate.swift中的applicationDidFinishLaunchingWithOptions中初始化Core Data堆栈,因为在尝试获取数据后会添加psc。
That boilerplate code from Apple includes: 苹果公司提供的样板代码包括:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
/* ... */
do {
try psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil)
} catch {
fatalError("Error migrating store: \(error)")
}
}
The saved data isn't available until the addPersistentStoreWithType
call finishes, and that's happening asynchronously on a different queue. 在
addPersistentStoreWithType
调用完成之前,保存的数据不可用,并且异步地在另一个队列上进行。 It'll finish at some point but your code above is executing before that happens. 它会在某个时候完成,但是上面的代码正在执行。 What you're seeing isn't surprising-- you're basically looping until the async call finishes.
您所看到的并不奇怪-您基本上在循环直到异步调用完成。
You need to somehow delay your fetch until the persistent store has been loaded. 您需要以某种方式延迟获取,直到永久性存储已加载。 There are a couple of possibilities:
有两种可能:
persistentStores
property to see if any stores have been loaded rather than repeatedly trying to fetch. persistentStores
属性,以查看是否已加载任何存储,而不是反复尝试获取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.