【发布时间】:2021-12-13 07:36:53
【问题描述】:
我有以下用于从 Firebase 实时数据库检索数据的快速代码:
private let db = Database.database().reference(withPath: "food")
func fetchFoods() {
db.getData(completion: { error, snapshot in
guard error == nil else {
print("ERROR")
print(error!.localizedDescription)
return
}
print(snapshot.value as? String ?? "Unknown")
// Food data is stored as a map of UUIDs to Food objects
guard let foodDicts = snapshot.value as? [String: Any] else {
print("ERROR: type cast failed")
return
}
})
}
但是我有一个奇怪的问题,如果我的数据是这样存储的:
{
"food": [{
"id": "8466A419-AE6E-4997-BE74-220B59C95F96",
"cholestrol": 0,
"macroProfile": {
"carbProfile": {
"sugar": 0,
"fiber": 0
},
...
}]
}
检索数据没有问题。但是,如果数据是这样存储的:
{
"food": {
"8466A419-AE6E-4997-BE74-220B59C95F96": {
"cholestrol": 0,
"macroProfile": {
"carbProfile": {
"sugar": 0,
"fiber": 0
},
...
}
}
}
我得到:
Unable to get latest value for query FQuerySpec (path: /, params: {
}), client offline with no active listeners and no matching disk cache entries
【问题讨论】:
-
将您的数据集树显示为图像。
标签: json swift firebase firebase-realtime-database