1
我試圖讓從具有節點多一個層次,並通過它不能得到一個節點的結果:火力地堡循環多級節點
我檢索這樣的數據(它完美,但不是爲節點所謂的 「項目」):
func fetchMeals() {
print("start pulling data")
let user = Auth.auth().currentUser?.uid
ref.child("Users_Food_Data").child(user!).queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
if snapshot.value as? [String : AnyObject] != nil {
let allMeals = snapshot.value as! [String : AnyObject]
self.foodArray.removeAll()
for (_, value) in allMeals {
let foodToShow = FoodStruct()
if let calories = value["calories"] as? String,
let itemCarbs = value["energy"] as? String,
let itemProteins = value["proteins"] as? String,
let itemFat = value["fat"] as? String,
let date = value["date"] as? String,
let id = value["id"] as? String,
let name = value["name"] as? String,
let interval = value["interval"] as? Int {
foodToShow.itemKcal = calories
foodToShow.itemCarbs = itemCarbs
foodToShow.itemProteins = itemProteins
foodToShow.itemFat = itemFat
foodToShow.id = id
foodToShow.interval = interval
foodToShow.date = date
foodToShow.itemName = name
self.foodArray.append(foodToShow)
self.collectionView?.reloadData()
}
self.breakfastArray.sort(by: {$0.interval! > $1.interval!})
}
}
})
}
和數據庫看起來是這樣的:
"Users_Food_Data" : {
"JztkBihGgda0jtSpe6pNwt8hZu13" : {
"Breakfast:23 Sep 2017" : {
"calories" : "1145.0",
"date" : "23 Sep 2017",
"energy" : "238.8",
"fat" : "3.0",
"id" : "-Kukx_9lSpCh3lcEMzap",
"interval" : 1.506207565807117E9,
"items" : {
"-KukxKAntXDaS__v3ZLA" : {
"calories" : "30",
"date" : "23 Sep 2017",
"energy" : "6",
"fat" : "0.1",
"interval" : 1.506207500336909E9,
"itemKey" : "-KukxKAntXDaS__v3ZLA",
"mealKey" : "-KukxKAntXDaS__v3ZLB",
"name" : "Strawberries",
"proteins" : "0.8",
"quantity" : "3"
},
"-KukxLYmyg32lU1D3Wh3" : {
"calories" : "29",
"date" : "23 Sep 2017",
"energy" : "9",
"fat" : "0.5",
"interval" : 1.506207505968336E9,
"itemKey" : "-KukxLYmyg32lU1D3Wh3",
"mealKey" : "-KukxLYmyg32lU1D3Wh4",
"name" : "Lemon",
"proteins" : "1.1",
"quantity" : "1"
}
},
"name" : "Breakfast",
"proteins" : "17.0"
},
"Breakfast:24 Sep 2017" : {
"calories" : "959.0",
"date" : "24 Sep 2017",
"energy" : "106.4",
"fat" : "46.1",
"id" : "-KunWOZeSxW9eCIA6O1z",
"interval" : 1.506250519537633E9,
"items" : {
"-KulrJq6jOpsG6oiJuDM" : {
"calories" : "458",
"date" : "24 Sep 2017",
"energy" : "4.6",
"fat" : "45",
"interval" : 1.506222704055992E9,
"itemKey" : "-KulrJq6jOpsG6oiJuDM",
"mealKey" : "-KulrJq6jOpsG6oiJuDN",
"name" : "Coconut",
"proteins" : "4",
"quantity" : "1"
},
我怎樣做,當我只知道 「Users_Food_Data」 和用戶ID。我只想將它們列在apropiate細胞中。
謝謝!我知道非規範化,我像你說的那樣構建我的數據。在這種情況下,我必須直接從節點獲取數據。我非常肯定這肯定是一種方式,但我無法弄清楚,現在我在這裏呆了兩天。你知道任何方式「像循環」如何在這種情況下使用它? –
好的。我編輯我的帖子旨在爲循環。 – Arrabidas92
謝謝!我如何在我的代碼中使用它來循環「項目」並獲得它們的值? –