我試圖爲我的Firebase數據庫中的一些數據創建完成處理程序。我試圖使用while循環,但沒有發生。代碼如下:在viewDidLoad中從谷歌Firebase獲取數據嘗試使用完成處理程序
var counter = 0
var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
while counter < 6 {
ifUserIsMember(counter: counter + 1) { (exist) ->() in
if exist == true {
print("Found something")
counter += 1
}
else {
print("NO DATA")
}
}
}
func ifUserIsMember(counter: Int, completionHandler: @escaping ((_ exist : Bool) -> Void)) {
let ref = FIRDatabase.database().reference()
ref.child("Test").child("\(counter)").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists(){
let value = snapshot.value as? NSDictionary
test1 = value?["cal1"] as! String
test2 = value?["cal2"] as! String
test3 = value?["cal3"] as! String
completionHandler(true)
}else{
print("user is not a member of a team")
completionHandler(false)
}
})
}
//稱這我試圖用一個while循環來獲取所有數據,但它不工作。它不會從循環中出來,並開始通過
你能分享數據庫結構嗎? –