2017-10-10 87 views
0

我想通過火力地堡看起來並檢查是否存在對孩子一定的值,如果這個值不存在。我想打印出來的一份聲明打印出火力子值

databaseRef.child("books").queryOrdered(byChild: "Category").queryEqual(toValue: categorySegue).observe(.childAdded, with: { (snapshot) in 

      if snapshot.exists() { 
       print("data found") 
      }else{ 
       print("no data found") 
      } 
}) 

當孩子值存在,它打印出的數據發現完全正常,但是當它不存在。它不會打印出任何數據發現

回答

1

這是因爲您看到.childAdded,只有火,如果有符合您查詢的孩子。

如果你也想檢測的情況不符合孩子的時候,你需要觀察.value事件和遍歷結果。

databaseRef.child("books").queryOrdered(byChild: "Category").queryEqual(toValue: categorySegue).observe(.childAdded, with: { (snapshot) in 

    if snapshot.exists() { 
     print("data found") 
     for child in snapshot.children { 
      print(child.key) 
     } 
    }else{ 
     print("no data found") 
    } 

}) 

另見Searching through child values Firebase/Swift