How to deal with saving two datatypes in firebase for same key?

200 Views Asked by At

I am trying to add last seen feature in my app using firebase. So I am trying to update my user's time stamp(each user has mTimeStamp key) value with ServerValue.TIMESTAMP using onDisconnect() method, and if the user comes online I am trying to update the user's mTimeStamp with the ValueEventListener on the reference .info/connected. So in one case the value updated is a Map<String,String> (which I have read somewhere that when it is retrieved back from Firebase database comes back as long primitive) and in one case it is saved as boolean.

public class PublicUserProfile {
    private int mAge,mSM;
    private double mPR;
   private Long mTimeStamp;
   private Boolean mIsOnline;
    public PublicUserProfile(){}

    public PublicUserProfile(int mAge, int mSM, double mPR, Long mTimeStamp) {
        Log.e("chimap","chad");
        this.mAge = mAge;
        this.mSM = mSM;
        this.mPR = mPR;
        this.mTimeStamp = mTimeStamp;
    }

    public PublicUserProfile(int mAge, int mSM, double mPR, Boolean mIsOnline) {
        Log.e("idhar aye","4 wale");
        this.mAge = mAge;
        this.mSM = mSM;
        this.mPR = mPR;
        this.mIsOnline = mIsOnline;
    }

    public PublicUserProfile(int mAge, int mSM, double mPR) {
        Log.e("idhar aye","3 wale");
        this.mAge = mAge;
        this.mSM = mSM;
        this.mPR = mPR;
    }

    public int getmAge() {
        return mAge;
    }


    public int getmSM() {
        return mSM;
    }


    public double getmPR() {
        return mPR;
    }

    public Long getmTimeStamp() {
        return mTimeStamp;
    }

    public Boolean getmIsOnline() {
        return mIsOnline;
    }
          }

Now I am trying to retrieve the data back from firebase using DataSnapshot.getValue(<T> class) method, now as mTimeStamp can only be saved as either long or as boolean in PublicUserProfile class I am getting DatabaseException error that class java.lang.Long cannot be converted to myPackagename.PublicUserProfile.

To counter this error I created an extra field in PublicUserProfile mIsOnlineso that the second constructor is called if the value is Boolean instead of Long but again I was getting the same DatabaseException error that java.lang.Long object can not be converted to PublicUserProfile even though at the same time I was looking in my database 'mTimeStamp' was set to true but I feel this error is reasonable because both in database and model class the name of key and field name should be same respectively. Also this approach has a problem because even if I try to save two keys in database i.e. one for long(to save timestamp(mTimeStamp)) and one for boolean (to save weather person is online or not(mIsOnline)) because when I call onDisconnect() method I can only do one operation i.e. either remove mIsOnline or setValue to mTimeStamp in database.

There is one more problem related to saving two keys i.e I want to retrieve 10 PublicUserProfiles and I want to show them in a RecyclerView in an order such that online people are shown first and then recent offline users and Firebase offers only one orderByValue() method so I can use only one key either mTimeStamp or mIsOnline.

How should I make my PublicUserProfile or how should I make calls to the database to add presence system in my app?

0

There are 0 best solutions below