有关Firebase团队建议的数据库结构的快速问题

时间:2019-07-29 16:15:52

标签: android firebase-realtime-database

我正在使用Firebase实时数据库来开发聊天应用程序。我阅读了firebase的官方文档,并看到了这种数据库结构

   public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private AlbumsAdapter adapter;
private List<Song> songList;
public SongCollection songCollection = new SongCollection();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    songList = new ArrayList<>();
    adapter = new AlbumsAdapter(this, songList);

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
    recyclerView.addItemDecoration(new MainActivity.GridSpacingItemDecoration(2, dpToPx(10), true));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(mLayoutManager);

        initialiseNames();
}
public void initialiseNames() {

    for (Song song : songCollection.getList()) {
        songCollection.add(song.getTitle());
        songCollection.add(song.getCoverArt());
    }
}




public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spacing;
            }
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}

/**
 * Converting dp to pixel
 */
private int dpToPx(int dp) {
    Resources r = getResources();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}

public void handleSelection(View view)
{
    String resourceId = AppUtil.getResourceId(this, view);

    Song selectedSong = songCollection.searchById(resourceId);

    AppUtil.popMessage(this, "Streaming song: " + selectedSong.getTitle());

    sendDataToActivity(selectedSong);
}

public void sendDataToActivity(Song song) {

    //New intent class to handle Song activities
    Intent intent = new Intent(this, PlaySongActivity.class);

    //Store Song information into the intent, to allow them to be called out
    intent.putExtra("id", song.getId());
    intent.putExtra("title", song.getTitle());
    intent.putExtra("artist", song.getArtist());
    intent.putExtra("fileLink", song.getFileLink());
    intent.putExtra("coverArt", song.getCoverArt());

    //Launch the Screen
    startActivity(intent);

}

我对此结构有一个简短的问题

使用这种结构,当我们需要显示所有对话时,我们需要读取{ // 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": { ... } } } 节点。每当消息最后一次更改时,如何更新chats节点的数据?据我所知,我们需要在chats节点上附加一个侦听器以获取最后一条消息,如果是的话,为什么不查询messages来获取它并显示在对话页面上呢?

1 个答案:

答案 0 :(得分:0)

好吧,您可以创建一个Firebase函数(触发器),并在聊天中侦听每条新消息,并在用户每次以lastMessage的形式发送消息时将其放入聊天中。是的,您可以按日期查询消息并获取最后一个客户端。
也许您可以这样:

-- chats
---- chatUUID1
------ members
-------- memberUUID1
-------- memberUUID2
-------- ...
------ messages
-------- messageUUID1
---------- messageData
-------- messageUUID2
---------- messageData
-------- ...
---- chatUUID2
---- ...

请记住,这取决于创建规则的方式,您可能需要保存用户正在为每个用户在某个地方参与的聊天或类似的操作。