0

I have a conversations node which contains straightforward subnodes such as displayMessage and conversationName, but also larger subnodes such as messages which hold info on all messages sent within the conversation. Is there a way to observe data only from specific subnodes, since I want to fetch data on the imageUrl, name, and displayMessage but I don't want it to fetch data on all the messages since with a giant list of messages it would unnecessarily use up a lot of data.

DB Structure

1 Answer 1

1

While the Firebase Database allows you to mix all kinds of content in a single branch, it is typically an anti-pattern if you have multiple entity types in a single branch.

In your example JSON, I see three main data types:

  1. The messages for a conversation
  2. The metadata for a conversation
  3. The members of a conversation

Typically you should model those in three separate top-level lists, where each has the same child keys (the conversation IDs) and then the specific data of that type for each conversation.

conversations
  conversation1: { conversationName: "", displayMessage: "",lastMessageTime: 1532968664.3149939 }
  conversation2: { conversationName: "", displayMessage: "" }
members
  conversation1: { ... }
  conversation2: { ... }
messages
  conversation1: { 
    "-Llg...": { ... },
    "DBC972...": { ... }
  }
  conversation2: { ... }
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.