繁体   English   中英

Swift -Firebase 如何知道 .childAdded 是否为空?

[英]Swift -Firebase how to know if .childAdded is empty?

我知道使用.value我可以使用.exists()检查是否有任何内容

在这种情况下,我在SearchController运行查询,并且需要返回多个结果。 一切正常的时候有结果,但如果没有任何我没有办法知道。

如何检查.childAdded是否.childAdded返回任何内容?

// if the user types in McDonalds and there are some then multiple will appear but if none then show a label that says “no results”

let searchText = searchController.searcchBar.text?.loweredCase() else { return }

Database.reference.child("restaurants")
    .queryOrdered(byChild:"restaurantName")
    .queryStarting(atValue: searchText)
    .queryEnding(atValue: searchText + “\u{f8ff}”)
    .observe( .childAdded, with: { (snapshot) in

          // no McDonalds appear so show a no results label
    })

由于.childAdded仅针对与查询匹配的现有子节点触发,因此您无法使用它来检测何时没有匹配项。 要检测是否没有匹配项,您需要使用.value侦听器并检查snapshot.exists

除了现有的.childAdded侦听器之外,您还可以使用它(Firebase 客户端会在后台对请求进行重复数据删除,因此不会传输额外的数据,或者您也可以使用.value侦听器来处理结果。在后一种情况下您将需要遍历snapshot.children以获取单个结果节点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM