0

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

        })
})

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"
        }
    }

 },

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.