Firebase中用于聊天应用程序的一对一对话结构

时间:2016-07-14 20:51:33

标签: json swift firebase firebase-realtime-database nosql

我正在尝试使用Firebase构建一对一会话节点,我建议我尝试存储显示特定用户对话所需的所有屏幕数据:

"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
      }
    }

    }

  }
}

我的目标是获取给定用户的对话并显示Facebook Messenger中的单元格(上一条消息,时间,看到的,其他用户图像和名称)

我的查询是:

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

        })

也尝试过:

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

        })
它应该给我一个对话,其中部分用户的给定ID,我做错了什么?

编辑:

"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
      }
    }

    }

  }
}

0 个答案:

没有答案