Reputation: 84
I have an AsyncTask
that is used when user wants to manually reload his discussion messages (fetches 20 newest messages of every discussion the user has from our server). For some reason all of the messages fetched this way get duplicated. And every run of the task just creates more duplicates. First run creates no duplicates, but every run after that does... It's worth mentioning that I use socket.io to communicate with the server so the responses fire a new thread when received.
Discussion class
@PrimaryKey
private String discussion_id;
private RealmList<Message> messages;
// More declarations + generated getters & setters
Message class
@PrimaryKey
private String msgID;
private String message;
//Declarations + getters & setters
And here's the simplified part in the AsyncTask that saves the message information into the database.
for (msgData : receivedMessages)
{
Message msg = new Message();
// Set all relevant data
realm.beginTransaction();
chat.getMessages().add(msg);
realm.copyToRealmOrUpdate(chat);
realm.commitTransaction();
}
realm.beginTransaction();
chat.setPreview(chat.getMessages().last().getMessage());
chat.setLastMessage(chat.getMessages().last().getCreated());
realm.copyToRealmOrUpdate(chat);
realm.commitTransaction();
realm.close();
I ran Log.d("MESSAGES TOTAL","=="+chat.getMessages().size());
after every call of the AsyncTask and this is what I got.
D/MESSAGES TOTAL: ==20
D/MESSAGES TOTAL: ==40
D/MESSAGES TOTAL: ==60
To be sure I ran
String id =chat.getMessages().first().getMsgID();
long count = realm.where(Message.class).equalTo("msgID",id).count();
Log.d("FIRST ITEM ID", id+"- COUNT:"+count);
And got
D/FIRST ITEM ID: 56bb693593de502003681054- COUNT:1
Now that I dwelled this deep into madness, I printed every message in a chat to console and looked at the ID's and this is what I found.
02-10 20:08:38.097 13339-13339/com.dev.proto D/DISCUSSIONACTIVITY:: CREATED: Wed Feb 10 16:45:41 EET 2016--MSG ID: 56bb693593de502003681054
02-10 20:08:38.097 13339-13339/com.dev.proto D/DISCUSSIONACTIVITY:: CREATED: Wed Feb 10 16:45:41 EET 2016--MSG ID: 56bb693593de502003681054
02-10 20:08:38.107 13339-13339/com.dev.proto D/DISCUSSIONACTIVITY:: CREATED: Wed Feb 10 16:45:41 EET 2016--MSG ID: 56bb693593de502003681054
Three identical message ids. Something isn't working as intended now... Any ideas on how to fix this or how to debug this further?
Upvotes: 1
Views: 339
Reputation: 3825
Each and every time you add Message object in Chat which is wrong you have to replace message object in case of object with same massage_id found
Discussion mDiscussion = realm.where(Discussion.class).equalTo("id", DiscussionID).findFirst();
Message mMessage = mDiscussion.getMessages().where().equalTo("id",updatedMessage.getId()).findFirst();
if(null!=mMessage&& mDiscussion.getMessages().indexOf(mMessage)!=-1){
//if Simmilar Message Object present in mDiscussion.getMessages() then replace
mDiscussion.getMessages().set(mDiscussion.getMessages().indexOf(mMessage),taskfinal);
}else{
//if not then add Message to list
mDiscussion.getMessages().add(taskfinal);
}
realm.copyToRealmOrUpdate(mDiscussion);
Hope this help you and other best of luck
Upvotes: 0