Firebase dataSnapshot 子节点
Firebase dataSnapshot sub-node
我正在尝试对用户的购物清单进行快照。快照工作正常,除了产品节点。
Link 到数据库:
这是我的观察员:
func observeLists(callBack: @escaping (_ list: [List]) -> Void){
let dbRef = database.child("list").child(ShareData.shared.userId)
dbRef.queryOrdered(byChild: "completed").observe(.value, with: { snapshot in
var newItems: [List] = []
for child in snapshot.children {
if let snapshot = child as? DataSnapshot {
let groceryItem = List(snapshot: snapshot)
newItems.append(groceryItem)
}
}
callBack(newItems)
})
}
这是我从 struct List
初始化(快照)
init(snapshot: DataSnapshot) {
let id = snapshot.key
let value = snapshot.value as? NSDictionary
let products = value?["products"] as? [Product] ?? []
let completed = value?["completed"] as? Bool ?? false
let storeId = value?["storeId"] as? String ?? ""
let name = value?["name"] as? String ?? ""
self.init(id, products, completed, storeId, name)
}
您数据库中的 products
节点不是产品数组 ([Product]
),而是 dictionary/map ([String: Product]
)。
我正在尝试对用户的购物清单进行快照。快照工作正常,除了产品节点。
Link 到数据库:
这是我的观察员:
func observeLists(callBack: @escaping (_ list: [List]) -> Void){
let dbRef = database.child("list").child(ShareData.shared.userId)
dbRef.queryOrdered(byChild: "completed").observe(.value, with: { snapshot in
var newItems: [List] = []
for child in snapshot.children {
if let snapshot = child as? DataSnapshot {
let groceryItem = List(snapshot: snapshot)
newItems.append(groceryItem)
}
}
callBack(newItems)
})
}
这是我从 struct List
初始化(快照)init(snapshot: DataSnapshot) {
let id = snapshot.key
let value = snapshot.value as? NSDictionary
let products = value?["products"] as? [Product] ?? []
let completed = value?["completed"] as? Bool ?? false
let storeId = value?["storeId"] as? String ?? ""
let name = value?["name"] as? String ?? ""
self.init(id, products, completed, storeId, name)
}
您数据库中的 products
节点不是产品数组 ([Product]
),而是 dictionary/map ([String: Product]
)。