Firebase .queryStarting无效(Swift)

时间:2017-11-19 22:58:06

标签: ios swift firebase firebase-realtime-database

即使有时间戳高于此值的消息,这也不会返回任何内容。

代码未正确排序:

let messagesRef = FIRDatabase.database().reference().child("all-messages").child(messageId).queryOrdered(byChild: "timestamp").queryStarting(atValue: 1511130644)

Firebase电话的完整代码:

FIRDatabase.database().reference().child("users").child(currentUserUid).child("timestampOfLastVisit").observeSingleEvent(of: .value, with: { (snapshot) in

        timestamp = snapshot.value as! NSNumber

        groupMessagesRef = FIRDatabase.database().reference().child("groups").child(groupId).child("messages")
        groupMessagesRef.observe(.childAdded, with: { (snapshot) in

            if self.sentMessage {
                let messageId = snapshot.key

                let messagesRef = FIRDatabase.database().reference().child("all-messages").child(messageId).queryOrdered(byChild: "timestamp").queryStarting(atValue: 1511130644)
                messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in ...

相关的Firebase JSON(显然是假数据):

{
  "all-messages" : {
    "-KzLOuvEdLWy7vfc2XsT" : {
      "fromId" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
      "groupId" : "-Kxk3kA9I8OEvNmFcavL",
      "isStarred" : true,
      "text" : "This should show",
      "timestamp" : 1511130640
    },
    "-KzLOw5OxZ4P_I0d70gZ" : {
      "fromId" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
      "groupId" : "-Kxk3kA9I8OEvNmFcavL",
      "isStarred" : false,
      "text" : "And so should this",
      "timestamp" : 1511130644
    },
    "-KzLS4uwDtpW6wHNxrmc" : {
      "fromId" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
      "groupId" : "-Kxk3kA9I8OEvNmFcavL",
      "isStarred" : false,
      "text" : "1",
      "timestamp" : 1511131471
    },
    "-KzLS5FvASnj_ky4WQkd" : {
      "fromId" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
      "groupId" : "-Kxk3kA9I8OEvNmFcavL",
      "isStarred" : false,
      "text" : "2",
      "timestamp" : 1511131472
    }
  },
  "groups" : {
    "-Kxk3kA9I8OEvNmFcavL" : {
      "groupCreator" : "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1",
      "groupDescription" : "Group",
      "groupImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchat1-a1da3.appspot.com/o/group_profile_images%2FA4DDA286-E7A3-4F96-ABF0-5717F4029C33.png?alt=media&token=6ad79728-74fd-405f-abbf-247dae4684b5",
      "groupMemberOneSignalIds" : {
        "5905a2f5-dfa3-46d7-964f-596bd2f2004a" : true,
        "67b9cb2a-6583-45b4-9fed-93cf333e9ca4" : true,
        "a1bf9ed9-0959-4c88-974b-5a829c1cdcc9" : true,
        "f2fb84b9-86af-40e7-8667-989a95b2e282" : true
      },
      "groupMembers" : {
        "3lQiuzudFGW51UwQ4Mseu8aOxyu2" : true,
        "JF7SCe3VUcWJi1ujumx0VpyaG5s1" : true,
        "YEbVehx0cqTvNaKrJTcs5GnRLGM2" : true,
        "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1" : true
      },
      "groupName" : "1",
      "groupTags" : {
        "-Kxk3kHxMuWQHEsaWN5k" : "1",
        "-Kxk3kHyMB2OL5pNAfW1" : "1",
        "-Kxk3kHyMB2OL5pNAfW2" : "1"
      },
      "messages" : {
        "-KzLOuvEdLWy7vfc2XsT" : 1,
        "-KzLOw5OxZ4P_I0d70gZ" : 1,
        "-KzLS4uwDtpW6wHNxrmc" : 1,
        "-KzLS5FvASnj_ky4WQkd" : 1
      },
      "starredMessages" : {
        "-KzLOuvEdLWy7vfc2XsT" : 1511204335
      }
    },
    "-Kxn_yTAZvkPls-_s8He" : {
      "groupCreator" : "JF7SCe3VUcWJi1ujumx0VpyaG5s1",
      "groupDescription" : "Group 3",
      "groupImageUrl" : "https://firebasestorage.googleapis.com/v0/b/groupchat1-a1da3.appspot.com/o/group_profile_images%2FD2C68A8F-33DC-43C6-8C99-762315760208.png?alt=media&token=aae87339-877e-484e-a0e1-a2e13455176c",
      "groupName" : "Group 3",
      "groupTags" : {
        "-Kxn_ygovR7mQIph3WiC" : "1",
        "-Kxn_ygovR7mQIph3WiD" : "1"
      }
    }
  },
  "users" : {
    "JF7SCe3VUcWJi1ujumx0VpyaG5s1" : {
      "email" : "Madi@gmail.com",
      "groups" : {
        "-Kxk3kA9I8OEvNmFcavL" : true,
        "-Kxk3n99i43MYECm1Ix8" : true,
        "-KxuBPAPBIcROMEtNujW" : true,
        "-KxuET1pZGg2x_aJvRkU" : true
      },
      "profileImageURL" : "https://firebasestorage.googleapis.com/v0/b/groupchat1-a1da3.appspot.com/o/profile_images%2F246CA55C-446D-4A24-8BE6-8B5B9C0F27AF.png?alt=media&token=47022ccd-1c92-460a-8630-e74a0d68bc4a",
      "searchUsername" : "madi",
      "timestampOfLastVisit" : 1511241228,
      "username" : "Madi"
    },
    "zhBZyAVGvAMZWi4QhvKDu7qb3Qr1" : {
      "email" : "Connor@gmail.com",
      "groups" : {
        "-Kxk3kA9I8OEvNmFcavL" : true,
        "-Kxk3n99i43MYECm1Ix8" : true
      },
      "profileImageURL" : "https://firebasestorage.googleapis.com/v0/b/groupchat1-a1da3.appspot.com/o/profile_images%2FBDC4020E-9F54-4F80-A4DC-668804215DE3.png?alt=media&token=ec2c8875-e6e4-40d5-8c07-3d743bf4ea16",
      "searchUsername" : "connor",
      "timestampOfLastVisit" : 1511205356,
      "username" : "Connor"
    }
  }
}

打印什么打印(快照):

Snap (-KzLOw5OxZ4P_I0d70gZ) <null>

打印(MESSAGEREF):

(/all-messages/-KzLOw5OxZ4P_I0d70gZ {
    i = timestamp;
    sp = 1511130644;
})

2 个答案:

答案 0 :(得分:0)

queryStartingAtValue()的第二个参数仅用于消除与第一个参数匹配的项之间的歧义。要按timestamp进行过滤,您应致电queryOrderedByChild("timestamp")

let messagesRef = 
  FIRDatabase.database().reference().child("all-messages").child(messageId)
             .queryOrdered(byChild: "timestamp")
             .queryStarting(atValue: 1511130644)

答案 1 :(得分:0)

目标是创建一个返回具有特定时间戳的节点的查询。

问题是你的代码查询的程度太深了。换句话说,您需要让Firebase迭代所有消息的子节点,直到找到具有子时间戳1511130644的子节点。因此它需要迭代

msg_0
msg_1
msg_2

您的代码试图过于具体,而且您要告诉它查看查询中数据的特定消息ID - 这没有任何意义,就像您知道确切的路径一样,您不会&#39 ; t需要查询!

这是一个常见的误解 - 只需记住,查询需要提供父节点,然后是你要查询的子节点,因为它将遍历父节点下面的child_nodes。

parent_node
  child_node
     child_node_of_what_you_are_querying
  child_node
     child_node_of_what_you_are_querying

以下是查询特定时间戳的代码 - 请注意不需要messageId

//self.ref is the firebase ref
let messagesRef = self.ref.child("all-messages").queryOrdered(byChild: "timestamp")
                                                .queryStarting(atValue: 1511130644)
messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in
    if snapshot.hasChildren() {
        print("got snap with children")
    } else {
        print("no snap found")
    }
})

如果您已经知道具体路径,可以直接观察。因此,如果您要打印某个消息的时间戳

let thisMsgRef = self.ref.child("all-messages").child(messageId)
let thisTimestampRef = thisMsgRef.child("timestamp")
thisTimestampRef.observeSingleEvent(of: .value) { snapshot in
    print(snap.value) //prints the timestamp
}