1

I am trying to structure a one to one conversation node With Firebase, as advised I am trying to store all data of the screen needed to display the conversation of a specific user:

"conversations": {
    "XDJKljQI8JeTk6xkyMfvMqbwbQR4": {
      "lastMessage": "my name is john",
      "timestamp": 1467849600000,
      "users":
        {
      "XDJKljQI8JeTk6xkyMfvMqbwbQR2": {
        "name" : "name",
        "profileImage" : "https://firebasestorage.googleapis.com",
        "country_code": "US",
        "seen" : false
      },
     "0CcKvNkOm5fVqLSNMBcdZV8Hx1c2": {
        "name" : "Anna",
        "profileImage" : "https://firebasestorage.googleapis.com",
        "country_code": "DE",
        "seen" : true
      }   
    }},
    "XDJKljQI8JeTk6xkyMfvMqbwbQR5": {
      "lastMessage": "my name is john",
      "timestamp": 1467849600000,

      "users":{
        "XDJKljQI8JeTk6xkyMfvMqbwbQR2": {
        "name" : "Dev",
        "profileImage" : "https://firebasestorage.googleapis.com",
        "country_code": "DK",
        "seen" : false
      },
      "XDJKljQI8JeTk6xkyMfvMqbwbQR3": {
        "name" : "Chris",
      "profileImage" : "https://firebasestorage.googleapis.com",
        "country_code": "US",
        "seen" : false
      }
    }

    }

  }
}

My goal is to get the conversation of a given user and display a cell like in Facebook messenger (last message, time, seen, the other user image and name)

My query is:

K.FirebaseRef.conversations
    .queryOrderedByChild("users")
    .queryStartingAtValue("0CcKvNkOm5fVqLSNMBcdZV8Hx1c2")
    .queryEndingAtValue("0CcKvNkOm5fVqLSNMBcdZV8Hx1c2")
    .observeSingleEventOfType(.Value, withBlock:{ snapshot in
        print(snapshot)

        })

also tried:

K.FirebaseRef.conversations
    .queryOrderedByChild("users")
    .queryEqualToValue("0CcKvNkOm5fVqLSNMBcdZV8Hx1c2")
    .observeSingleEventOfType(.Value, withBlock:{ snapshot in
        print(snapshot)

        })

it should give me a conversation where the given id of the user in part of, what am I doing wrong ?

Edit:

"conversations": {
    "user1_user2": {
      "lastMessage": "my name is john",
      "timestamp": 1467849600000,
      "users":
        {
      "user1": {
        "name" : "John",
        "profileImage" : "https://firebasestorage.googleapis.com",
        "country_code": "US",
        "seen" : false
      },
     "user2": {
        "name" : "Dev",
        "profileImage" : "https://firebasestorage.googleapis.com",
        "country_code": "DE",
        "seen" : true
      }   
    }},
    "user1_user3": {
      "lastMessage": "my name is john",
      "timestamp": 1467849600000,

      "users":{
        "user1": {
        "name" : "John",
        "profileImage" : "https://firebasestorage.googleapis.com",
        "country_code": "US",
        "seen" : false
      },
      "user3": {
        "name" : "Chris",
      "profileImage" : "https://firebasestorage.googleapis.com",
        "country_code": "US",
        "seen" : false
      }
    }

    }

  }
}
Community
  • 1
  • 1
iOSGeek
  • 5,115
  • 9
  • 46
  • 74
  • Instead of using "random" room IDs, base the room ID on the users in it. See http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Jul 14 '16 at 23:57
  • @FrankvanPuffelen I have adopted your approach (see edit above) but still can't find a way to get all conversations of a given user, for example how to get all conversation of user1 ? – iOSGeek Jul 16 '16 at 10:26
  • 1
    In NoSQL you typically model your data for the way your app consumes it (see this article on [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/)). So if you app needs a list of all conversations for a user, consider storing a list of all conversations for each user `/users//conversations/`. – Frank van Puffelen Jul 16 '16 at 14:25

0 Answers0