2017-04-26 39 views
0

我試圖爲我的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循環來獲取所有數據,但它不工作。它不會從循環中出來,並開始通過

+0

你能分享數據庫結構嗎? –

回答

1

你的代碼有一些增加。應工作

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) 
     } 
    }) 
} 

//稱這在viewDidLoad中

var counter = 0 
var ref: FIRDatabaseReference! 
ref = FIRDatabase.database().reference() 

while counter < 6 { 
    ifUserIsMember(counter: counter + 1, 
        completionHandler: { existing in 
     if existing { 
      print("Found something") 
     } else { 
      print("NO DATA") 
     } 
    }) 

    counter += 1 // or you will have infinite loop 
} 

如果要加載對象的數組,爲的tableView例如,你應該與完成處理一個又一個的功能。類似於

// for example it will return [String] 
func getAllObjects(completion: (_ hasFinished: [String]) -> Void) { 
    var arrayToReturn = [String]() 
    var countOfChecked = 0 
    while counter < 6 { 
     ifUserIsMember(counter: counter + 1, 
         completionHandler: { existing in 
      var countOfChecked += 1 
      if existing { 
       print("Found something") 
       arrayToReturn(//append some data) 
       if countOfChecked == 6 { // its your number (6) 
        completion(arrayToReturn) 
       } 
      } else { 
       print("NO DATA") 
      } 
     }) 

     counter += 1 // or you will have infinite loop 
    } 
} 

就像這樣。你應該明白這個主意。

希望它有幫助

1

我不是很熟悉Firebase,我不確定你想要完成什麼,但在完成處理程序中更新counter不會上班。

如果您在ifUserIsMemberFunc的頂部放置了一條日誌語句,如:print("counter: \(counter)"),那麼您可能會對輸出感到驚訝。

如果我正確地讀取了您的代碼,您基本上會有一個無限循環創建Firebase查詢以檢查1中是否存在子代。

如果您想要並行運行這些查詢,您需要將counter += 1置於完成塊的外部之外。但是,那麼您需要等待所有查詢完成。 (DispatchGroup是個不錯的選擇。)

如果你想做別的事情,還有其他的選擇。希望這可以幫助!

相關問題