Search code examples
javaandroidrealmrealm-mobile-platform

Retreive the chat history of a single person using realm


I am trying to make chat app, where I am storing the chat history in the database using realm. I am storing the User's id of mine and other person id into the database. How can I get the chat history of mine and other person data? This is the query I am using but I am getting only the my chat not the other ones .

chathistorylist = chatrealm.where(Chat_history_pojo.class)
                     .equalTo("senderid", Session.getUserID(getApplicationContext()))
                     .equalTo("receiverid", getIntent().getStringExtra("rid"))
                     .findAll();

And below is the data i am storing the message i have sent to other person

chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);
chatupdatepojo.setSenderid(Session.getUserID(getApplicationContext()));
chatupdatepojo.setReceiverid(getIntent().getStringExtra("rid"));                       
chatupdatepojo.setMessage(sendmessgae.getText().toString());
chatupdatepojo.setType("text");
chatrealm.commitTransaction();
chatrealm.close();
sendmessgae.setText("");

This is how the i am storing of the other person message into database

chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);

chatupdatepojo.setSenderid(getIntent().getStringExtra("rid"));
chatupdatepojo.setReceiverid(Session.getUserID(getApplicationContext()));
chatupdatepojo.setMessage(intent.getStringExtra("msg"));
chatupdatepojo.setType("text");
chatrealm.commitTransaction();
chatrealm.close();

This is the POJO Class

public class Chat_history_pojo  extends RealmObject{
    String senderid;
    String receiverid;
    String message;
    String type;
    String sender_profile;
    String reciever_profile;

    public String getSender_profile() {
        return sender_profile;
    }

    public void setSender_profile(String sender_profile) {
        this.sender_profile = sender_profile;
    }

    public String getReciever_profile() {
        return reciever_profile;
    }

    public void setReciever_profile(String reciever_profile) {
        this.reciever_profile = reciever_profile;
    }

    public String getSenderid() {
        return senderid;
    }

    public void setSenderid(String senderid) {
        this.senderid = senderid;
    }

    public String getReceiverid() {
        return receiverid;
    }

    public void setReceiverid(String receiverid) {
        this.receiverid = receiverid;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

Solution

  • Have you tried

    chathistorylist = chatrealm.where(Chat_history_pojo.class)
                         .beginGroup()
                         .equalTo("senderid", Session.getUserID(getApplicationContext()))
                         .equalTo("receiverid", getIntent().getStringExtra("rid"))
                         .endGroup()
                         .or()
                         .beginGroup()
                         .equalTo("receiverid", Session.getUserID(getApplicationContext()))
                         .equalTo("senderid", getIntent().getStringExtra("rid"))
                         .endGroup()
                         .findAll();
    

    ?