請解釋在哪裏(方法或可能存在與競爭方法觀察)我需要從Firebase檢索數據。什麼是最佳實踐?不能找到堆棧工作答案:如何知道Firebase何時檢索到數據
var ref:FIRDatabaseReference!
ref = FIRDatabase.database().reference()
//filling ShopList by favorite
ref.child("goods").observeSingleEvent(of:.value, with: {(snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
print("my app: 1. Start ObserveSingle")
for snap in snapshots {
let g = Good()
g.barcode = String(describing: snap.childSnapshot(forPath: "barcode").value!)
g.name = snap.childSnapshot(forPath: "name").value! as! String
g.price = snap.childSnapshot(forPath: "minprice").value! as! Double
g.favorite = snap.childSnapshot(forPath: "favorite").value! as! Bool
if g.favorite {//избранные товары
DataService.dataService.dbShopList.append(g)
}
}
print("my app: 2. Observe finished \(DataService.dataService.dbShopList.count)")
}
})
print("my app: 3. observe finished \(DataService.dataService.dbShopList.count)")
如何理解何時何地完成第2步
調試下面
my app: 3. observe finished 0
my app: 1. Start ObserveSingle
my app: 2. Observe finished 3
什麼是你想用問嗎?您的印刷品會在事情發生時顯示給您 – jonrsharpe