Eric
Eric

Reputation: 197

Fetch specific subnode data from a node (Firebase, Swift)

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

Upvotes: 0

Views: 41

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599001

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: { ... }

Upvotes: 1

Related Questions