Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase queryOrderedByChild QueryQualtValue不工作_Firebase_Firebase Realtime Database - Fatal编程技术网

Firebase queryOrderedByChild QueryQualtValue不工作

Firebase queryOrderedByChild QueryQualtValue不工作,firebase,firebase-realtime-database,Firebase,Firebase Realtime Database,无法让我的iOS项目查询我的玩家朋友高分: private func checkFriendsScores(fbid: Int64) { let users = ref.child("users") let fbidQuery = users.queryOrderedByChild("FBID") let friendIdNumber = String(fbid) let queryFriend = fbidQuery.queryEqualToValue(frie

无法让我的iOS项目查询我的玩家朋友高分:

private func checkFriendsScores(fbid: Int64)
{
    let users = ref.child("users")
    let fbidQuery = users.queryOrderedByChild("FBID")
    let friendIdNumber = String(fbid)
    let queryFriend = fbidQuery.queryEqualToValue(friendIdNumber)
    queryFriend.observeSingleEventOfType(.Value) { [unowned self](snapshot: FIRDataSnapshot!) in
        self.updateFriendsScores(fbid, data: snapshot)
    }
}
该代码编译和运行良好,应用程序的其他部分也正常工作。查询应该找到具有给定FBID的朋友,然后启动updateFriendScores函数,但该块永远不会执行

数据如下所示:

{
  "users" : {
    "WhiPPS6AbcX6Xp2XQft29JZXR9G3" : {
      "BK035" : [ null, 540, 466, 515, 576, 748, 1019, 765, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
      "FBID" : 10208576492403564
    },
    "d0fT2ge6ALa6QUKFcRApiqjaJd82" : {
      "BK018" : [ null, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646, 646 ],
      "BK035" : [ null, 631, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
      "FBID" : 1652501845074069
    },
    "o67iatcYLRbd2CrgkXGQWZNpmyC3" : {
      "BK018" : [ null, 816, 715, 642, 690, 864, 745, 899, 858, 847, 1045, 1148, 1002, 1119, 833, 1067, 0, 0, 0, 0, 0 ],
      "BK035" : [ null, 826, 620, 657, 673, 810, 1090, 804, 932, 931, 1079, 1007, 972, 1098, 1065, 927, 1582, 1195, 1265, 1366, 1548 ],
      "FBID" : 1094051894000026
    },
    "yW5ntKW9pwYvwm2EyvJlGSTzm0M2" : {
      "BK018" : [ null, 763, 457, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
      "BK035" : [ null, 645, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
      "FBID" : 10153916083832933
    }
  }
}

有什么想法吗?

正如我在您的数据中看到的,您没有将
FBID
保存为
字符串
,因此在执行
queryqualtovalue
时,无需将其强制转换为字符串

private func checkFriendsScores(fbid: Int64){
    let users = ref.child("users")
    let fbidQuery = users.queryOrderedByChild("FBID")
    let queryFriend = fbidQuery.queryEqualToValue(Int(fbid))
    queryFriend.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        let BK035 = snapshot.value!["BK035"] as! NSArray
        self.updateFriendsScores(fbid, data: snapshot)
    }) { (error) in
        print(error.localizedDescription)
    }
}

问题是您正在将Int64转换为字符串以执行查询。12345的字符串与12345的值不同

推理是合理的,因为QueryQualtOvalue需要AnyObject,而Int64不符合AnyObject

但是,将其转换为字符串将使其成为字符串,而不是值,并作为值存储在Firebase中

因此,解决方案是

a) Use Int values in general instead of converting to strings
b) Type conversion to an Int. let num = Int(fbid) and then 
       fbidQuery.queryEqualToValue(num)
c) Avoid the issue completely any store/retrieve those numbers as a string in Firebase

我选择c),因为从长远来看,它只会使处理数据变得更加容易。

问题原来是安全规则。以下是对我有效的规则,其中有一条注释显示了我必须添加一行的位置

既然@Jay有一个很好的答案可以帮助保持数据的正常(尽管这不是问题的根本原因),我也投了赞成票

抱歉没有提前发布安全规则。我应该想到这一点

// These rules grant write access to a "users" node matching the authenticated
// user's ID from the Firebase auth token; and read access to all "users" nodes
// Since we will look up our friends via their Facebook ID's we need to index on
// that value
{
  "rules": {
    "users": {
      ".read": "auth != null",       // <<< Had to add this line
      ".indexOn": "FBID",
      "$uid": {
        ".read": "auth != null",       // <<< This was not enough
        ".write": "$uid === auth.uid"
      }
    }
  }
}
//这些规则授予与经过身份验证的
//Firebase身份验证令牌中的用户ID;和对所有“用户”节点的读取访问
//因为我们将通过朋友的Facebook ID查找他们,所以我们需要对他们进行索引
//那个值
{
“规则”:{
“用户”:{

.read:“auth!=null”,//Int64可能不是处理该变量的最佳方式。Swift中有两种类型的任何“东西”,任何可以是类、stuct或enum,或者是专门用于类类型的任何对象。QueryQualtOvalue需要任何对象,而Int64不符合任何对象,因为它不是类,而是值类型。任何对象都可以pear可以保存Int,但在这些情况下,任何对象都会在幕后隐式地将Int转换为_nscfn数字。因此,这需要是一个字符串,或者转换为Int:let num=Int(fbid),然后:fbidQuery.QueryQuiltoValue(num)@Jay谢谢Jay。考虑到您的想法,刚刚编辑了答案。