【发布时间】:2017-06-28 13:18:43
【问题描述】:
我对 CoreData 有一个奇怪的问题。当我第一次加载数据时,一切似乎都正常,但在保存数据后,所有后续的 fetch 请求都停止工作(或者更确切地说返回空)。
我从 this 项目复制了 CoreDataStack,这是 udacity 的一个示例项目。它包含一个保存辅助函数。
func save() {
context.performAndWait() {
if self.context.hasChanges {
do {
try self.context.save()
} catch {
fatalError("Error while saving main context: \(error)")
}
// now we save in the background
self.persistingContext.perform() {
do {
try self.persistingContext.save()
} catch {
fatalError("Error while saving persisting context: \(error)")
}
}
}
}
}
在我的视图控制器内部,我可以使用如下代码为不同的谓词正常运行获取请求。
fetchedResultsController?.fetchRequest.predicate = NSPredicate(format: "fromDatetime >= %@ AND fromDatetime < %@", argumentArray: [selectionTime, selectionTime + 3600_000])
fetchedResultsController?.managedObjectContext.refreshAllObjects()
let m = fetchedResultsController?.fetchedObjects as! [Measurement]
print("number of elements in m: \(m.count)")
使用测试按钮保存数据后。
@IBAction func testAction(_ sender: Any) {
// test action
print("test clicked")
let delegate = UIApplication.shared.delegate as! AppDelegate
let stack = delegate.stack
stack.save()
}
所有后续的获取请求都是空的。我还创建了另一个 ViewController,它实现了UITableViewController,包含存储在设备上的所有数据。如果我打开它,数据仍然在那里正确显示,即使在保存之后也是如此。
什么可能导致这种行为?
【问题讨论】: