0

In my Firebase database I use a compound key as the ID for one-to-one conversations in the following format 'UID1_UID2' sorted lexicographically.

When I want to load all conversations via the iOS client for a particular user (UID1) how can I query Firebase when the compound key consists of two combined user IDs.

adjuremods
  • 2,938
  • 2
  • 12
  • 17
jeh
  • 2,373
  • 4
  • 23
  • 38

2 Answers2

1
queryStartingAt("UID1_").queryEndingAt("UID1_")

will return all conversations UID1_ started.

You should be structuring your data as

conversation:  UID1_UID2
Jay
  • 34,438
  • 18
  • 52
  • 81
0

Compound keys are not that great of an idea. Please consult this part of the documentation that explains how to best model a chat / conversation structure.

Relevant excerpt demonstrating how to structure data for chats:

{
  // Chats contains only meta info about each conversation
  // stored under the chats's unique ID
  "chats": {
    "one": {
      "title": "Historical Tech Pioneers",
      "lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
      "timestamp": 1459361875666
    },
    "two": { ... },
    "three": { ... }
  },

  // Conversation members are easily accessible
  // and stored by chat conversation ID
  "members": {
    // we'll talk about indices like this below
    "one": {
      "ghopper": true,
      "alovelace": true,
      "eclarke": true
    },
    "two": { ... },
    "three": { ... }
  },

  // Messages are separate from data we may want to iterate quickly
  // but still easily paginated and queried, and organized by chat
  // conversation ID
  "messages": {
    "one": {
      "m1": {
        "name": "eclarke",
        "message": "The relay seems to be malfunctioning.",
        "timestamp": 1459361875337
      },
      "m2": { ... },
      "m3": { ... }
    },
    "two": { ... },
    "three": { ... }
  }
}
pkluz
  • 4,871
  • 4
  • 26
  • 40
  • I saw this then I read this http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase in regard to one to one structure. Looks like I need to add an index for your approach to access the chats the user is part of so will try that approach. – jeh Nov 16 '16 at 19:27
  • The problem with compound keys is that they - by design - are very limiting. Namely: A<>B != B<>A, and they don't scale beyond two participants if by any chance at some point you turned out to need it. Sure, they're pragmatic and will get you started quicker, I do however not recommend constraining yourself like this – pkluz Nov 16 '16 at 19:45