Firebase在查询为空时 [英] Firebase when query is empty

查看:108
本文介绍了Firebase在查询为空时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用firebase执行查询来检查是否有任何具有特定值的子对象。问题是如果没有这样的对象代码不运行。所以在下面的代码中,如果没有包含 facebookID 的子元素 17 ,那么剩下的代码永远不会运行。有没有一种方法来确定查询何时发现 0 结果?

  func previousUser(completion:(result:Bool) - > Void){

var queryFound = false
print(check if user is previous)
print(UserData。 globalfacebookID)
let ref = Firebase(url:https://amber-torch-556.firebaseio.com/Users)
ref.queryOrderedByChild(facebookID)。queryEqualToValue(17).observeSingleEventOfType (.ChildAdded,withBlock:{snapshot in

print(snapshot.childrenCount)
print(query has ran)
UserData.globalparseID = snapshot.key

queryFound = true
completion(result:queryFound)
$ b $)


<



试试这个

  ref.queryOrderedByChild(facebookID)。queryEqualToValue(17)
.observeEventType(.Value,withBlock:{snapshot in

如果snapshot.value是NSNull {
print(dude,snapshot was null)
} else {
print(snapshot.key)
}






将.ChildAdded更改为.Value,然后检查NSNull


I perform a query using firebase to check if there are any objects that have a child with a certain value. The problem is if there are no such objects the code doesn't run. So in the code below if there are no objects that have a child with facebookID that is 17 the rest of the code will never run. Is there a way to determine when a query finds 0 results?

func previousUser(completion:(result:Bool)->Void){

    var queryFound = false
    print("check if user is previous")
    print(UserData.globalfacebookID)
    let ref = Firebase(url:"https://amber-torch-556.firebaseio.com/Users")
    ref.queryOrderedByChild("facebookID").queryEqualToValue(17).observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in

    print(snapshot.childrenCount)
    print("query has ran")
    UserData.globalparseID = snapshot.key

    queryFound = true
    completion(result:queryFound)

})

解决方案

Check your ref to make sure it's valid.

and try this

        ref.queryOrderedByChild("facebookID").queryEqualToValue(17)
           .observeEventType(.Value, withBlock: { snapshot in

              if snapshot.value is NSNull {
                 print("dude, snapshot was null")
              } else {
                 print(snapshot.key)
              }

        })

Change .ChildAdded to .Value and then check for NSNull

这篇关于Firebase在查询为空时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆