android - Proper way of handling messages in chat -
i have added chat feature app. storing messages , timestamps in firebase database
.
i trying figure out way of displaying (in current chat room) last (let's say) 60 messages. mean retrieving last 60 items of current conversation firebase using limittolast()
.
furthermore add load more
button appear when 60 messages limit has been reached swiping , should load 60.
is there proper way of handling message archive stated above?
using code below retrieve whole specific message archive. find ineffective when specific chat archive has thousands of messages.
mchildeventlistener = new childeventlistener() { @override public void onchildadded(datasnapshot datasnapshot, string s) { chatmessage chatmessage = datasnapshot.getvalue(chatmessage.class); mmessageadapter.add(chatmessage); } @override public void onchildchanged(datasnapshot datasnapshot, string s) { } @override public void onchildremoved(datasnapshot datasnapshot) { } @override public void onchildmoved(datasnapshot datasnapshot, string s) { } @override public void oncancelled(databaseerror databaseerror) { } }; mdatabasereference.child("chat_messages").child(chatid).addchildeventlistener(mchildeventlistener);
to achieve this, recomand using following logic:
pageendoffset = 0; pagelimit = 60; pageendoffset += pagelimit; query query = mdatabasereference.child("chat_messages") .orderbychild("chatid").limittofirst(pagelimit) .startat(pageendoffset); query.addvalueeventlistener(youractivity.this);
hope helps.
Comments
Post a Comment