2

I have a DB in Firebase with this structure:

{
  "chats" : {
    "-L-hPbTK51XFwjNPjz3X" : {
      "lastMessage" : "Hello!",
      "timestamp" : 1512590440336,
      "title" : "chat 1",
      "users" : {
        "Ol0XhKBksFcrYmF4MzS3vbODvT83" : true
      }
    }
  },
  "messages" : {
    "-L-hPbTK51XFwjNPjz3X" : {
      "-L-szWDIKX2SQl4YZFw9" : {
        "message" : "Hello!",
        "timestamp" : 1512784663447,
        "userId" : "Ol0XhKBksFcrYmF4MzS3vbODvT83"
      }
    }
  },
  "users" : {
    "Ol0XhKBksFcrYmF4MzS3vbODvT83" : {
      "chats" : {
        "-L-hPbTK51XFwjNPjz3X" : true
      },
      "email" : "mm@gmail.com",
      "name" : "mm"
    }
  }
}

My code:

Database.database().reference().child("chats")
  .queryOrdered(‌​byChild: "users/(userId)").queryEqual(toValue: true).observe(.value, with: { snapshot in .... }

When I try to get chat members or user chats, It shows this warnings:

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

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

I found lots of solutions but anything works fine for me. I want to define IndexOn rules in my DB, Can you help me?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • What code triggers that warning? – Frank van Puffelen Dec 09 '17 at 17:16
  • @FrankvanPuffelen This is the code: Database.database().reference().child("chats").queryOrdered(byChild: "users/\(userId)").queryEqual(toValue: true).observe(.value, with: { snapshot in .... } – Roberto Gómez Dec 09 '17 at 17:21
  • While it is possible to add an index for that user, it means you'd have to add more indexes as you add users to the app. This is unmaintainable. Instead consider adding an inverted index to your data structure: an additional list that contains the chats for a specific user. This type of duplication is quite common. See my answer here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Dec 09 '17 at 17:25
  • Ok! I try to do this by adding a list of chat ids in User data (it calls "chats", you can see). I need to put there all the chats information?? – Roberto Gómez Dec 09 '17 at 17:49
  • If you want to show the chats for a user, you start by loading `Database.database().reference("users").child(userId).child("chats")`. Then for each chat Id under that, you load the actual chat data from `/chats`. This is known as a client-side join. – Frank van Puffelen Dec 09 '17 at 17:59
  • Hi @FrankvanPuffelen, I made your suggestion solution, but the message: "Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "chats/-L-hPbTK51XFwjNPjz3X" at /users to your security rules for better performance." still appear :( – Roberto Gómez Dec 17 '17 at 11:03
  • That sounds like you're still querying across two levels with dynamic keys. See https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Dec 17 '17 at 15:22

0 Answers0