gpt4 book ai didi

使用未指定索引的 Swift Firebase

转载 作者:行者123 更新时间:2023-11-28 15:10:17 26 4
gpt4 key购买 nike

所以,我有一个聊天应用程序,我的数据结构如下

chats : {
chat12345 : {
members : {
user123 : true,
user456 : true,
}
}
}

users : {
user123 : {
chats : {
chat12345 : true
}
}
}

所以当我想获取用户的聊天列表时,我只需使用:

let ref = dbref.child("users").child("user123").child("chats")
ref.observe(.childAdded, with: { (snapshot) -> Void in

let chatRef = dbref.child("chats").child(snapshot.key)

chatRef.observeSingleEvent(of: .value, with: { (chatSnapshot) in

//I now have the chat object

})
})

所以你在上面看到,我能够查询每个聊天 ID 以成功从 Firebase 获取聊天对象。

但我还需要观察我加入的聊天的变化,例如聊天标题、图像或其他属性是否发生变化。

所以当我尝试以下操作时:

let queryRef = chatRef.queryOrdered(byChild: "members/user123").queryEqual(toValue: true)

queryRef.observe(.childChanged, with: { (snapshot) -> Void in

})

这在技术上可行,但控制台输出以下内容:

Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "members/user123" at /chats to your security rules for better performance.

如何修改安全规则或我的查询以观察我加入的聊天的变化?

这是我现在的安全规则:

"chats": {

".read": "auth != null",
".write": "auth != null",

"$chatId" : {
"members": {
".indexOn": ".value"
}
}

},

最佳答案

你不应该 queryOrderedBy(/key/$variable) 你需要构建你的数据,就像你的变量留在 chatRef 一样。然后你可以使用静态索引。

关于使用未指定索引的 Swift Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47769044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com