使用未指定索引的Swift Firebase [英] Swift Firebase using an unspecified index

查看:40
本文介绍了使用未指定索引的Swift Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个聊天应用程序,我的数据的结构是这样的

So, I have a chat app and my data is structured like so

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

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

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

So when I want to grab a list of the user's chats, I simply use:

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获取聊天对象.

So you see above, that I am able to query each chat ID to get the chat object from Firebase successfully.

但是我还需要观察我所参与的聊天的变化,例如,如果聊天标题,图像或其他属性发生变化.

But I also need to observe changes to chats that I am a member of, for instance if the chat title, image, or other properties change.

因此,当我尝试以下操作时:

So when I try the following:

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

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

})

从技术上讲,这是可行的,但是控制台会输出以下内容:

This technically works, but the console outputs the following:

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.

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

How do either modify security rules or my query to observe changes to chats of which I am a member?

这是我现在的安全规则:

Here are my security rules right now:

"chats": {

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

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

 },

推荐答案

您不应使用queryOrderedBy(/key/$ variable)来构造数据,例如将变量保留在chatRef上.然后,您可以使用静态索引.

you should not queryOrderedBy(/key/$variable) you need to structure your data something like your variables stay at chatRef. Then you could use static indexes.

这篇关于使用未指定索引的Swift Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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