我查看了不同的帖子,试图将保存核心数据的原因归结为不适合我。
我的实体名称是 [ImportantData] ,其中2个属性 [attribute1] [attribute2] 在字符串中设置("是"或"不")。
我使用firebase查看值是否已更改。 Firebase名称为 [" FirebaseName"] ,其中包含 [" firebaseChild"]
以下功能在AppDelegate的' performFetchWithCompletionHandler' 中调用。
var importantData = [ImportantData]()
var refFetchWorking: DatabaseReference!
func isFetchWorking() {
refFetchWorking = Database.database().reference().child("FirebaseName")
refFetchWorking?.observeSingleEvent(of: .value, with: {(snapshot } in
let dict = snapshot.value as! [String: Any]
let fetch = dict["firebaseChild"] as! String
let context = (UIApplication.shared.delegate as! AppDelegate).persistantContainer.viewContext
let data = ImportantData(context:context)
let attribute1 = data.attribute1
let attribute2 = data.attribute2
do {
self.attribute1 = try context.fetch(ImportantData.fetchRequest())
print(attribute1!, attribute2!, "fetch")
} catch {
print("fail fetched"
}
if fetch != "closed" {
data.setValue("yes", forKey: "attribute1"
data.setValue("yes", forKey: "attribute2"
do {
try context.save()
print(attribute1!, attribute2!, "test")
} catch {
print("error after test"
}
} else {
print ("is closed!")
}
do { context.save()
print(attribute1!, attribute2!, "after save")
} catch {
print("error saving)
}
})
}
NSLog正在打印:
" no no fetch"
" no no test"
"保存后没有#34;
但是获取!="已关闭"
非常感谢任何帮助!谢谢!