Ian
Ian

Reputation: 157

How to parse data in RealmObject setters?

What I want to do is make a custom setter method for a RealmOblject subclass. Realm doesn't allow us to use custom logic in getters and setters so the only available options are workarounds. There are 2 ways that I am aware of

1) Using @Ignore as explained in Realm documentation ( http://realm.io/docs/java/0.80.0/ ).

2) The second way is to make a wrapper class that contains the RealmObject and must implement all of its methods

Eg. Realm model for Score

public class Score extends RealmObject
{
    private int score;

    public int getScore()
    {
        return score;
    }

    public void setScore(int score)
    {
        this.score = score;
    }
}

Wrapper for Score

public class ScoreWrapper
{
    private Score wrappedScore;
    public int getScore()
    {
        return wrappedScore.getScore();
    }

    public void setScore(int score)
    {
        //can do any custom data parsing here
        wrappedScore.setScore(score+1);
    }

    public RealmObject getRealmObject()
    {
        return wrappedScore;
    }

}

Method 1) breaks object encapsulation so definitely not going to go do this way. Method 2) does seems like a good compromise. I still feel like it is an abuse of "object oriented" DBMS though. What would be the best way for me to do this?

Upvotes: 1

Views: 785

Answers (2)

Christian Melchior
Christian Melchior

Reputation: 20126

Christian from Realm here. The getters and setters are the weak point of how Realm works currently, but static methods are allowed, so the current best practise, that require the least amount of code is something like this:

public class Score extends RealmObject
{
    ...

    public static void incrementScore(int score, Score score) {
      score.setScore(score + 1);
    }
}

Score obj = new Score();
Score.updateScore(42, obj);

We are actively working on fixing this though, so you can follow any progress here: https://github.com/realm/realm-java/issues/909

EDIT: From Realm Java 0.88.0 and forward you can now use methods as normal with Realm, so the above could be rewritten to:

public class Score extends RealmObject {
    private int score;

    public void incrementScore() {
      score = score + 1;
    }
}

Score obj = new Score();
obj.incrementScore();

Upvotes: 3

Yogendra
Yogendra

Reputation: 5288

Please initialize Score class object in your wrapper class.

public class ScoreWrapper
{
    private Score wrappedScore = getRealmObject();

    public int getScore()
    {
        return wrappedScore.getScore();
    }

    public void setScore(int score)
    {
        //can do any custom data parsing here
        wrappedScore.setScore(score+1);
    }

   public RealmObject getRealmObject()
    {
   if(wrappedScore==null)
wrappedScore = new Score();
        return wrappedScore;
    }

}

//==============================================//

And set value from another class:

ScoreWrapper wrapper = new  ScoreWrapper ();

wrapper.setScore(scoreValue);

Upvotes: 0

Related Questions