nasjgby
nasjgby

Reputation: 27

Data change in realm android

In my android app, I persist a workout object to realm. In one of my activities, I create an object with this code:

realm.beginTransaction();

Workout w = realm.createObject(Workout.class);

w.setmWorkoutId(UUID.randomUUID().toString());

realm.commitTransaction();

Here is my workout class:

public class Workout extends RealmObject{
  private String mWorkoutId;
  private int restSecsLeft;
  private boolean prevSetOver = true;
  private boolean workoutOver = false;

  public Workout(){}

  public String getmWorkoutId() {
    return mWorkoutId;
  }

  public void setmWorkoutId(String mWorkoutId) {
    this.mWorkoutId = mWorkoutId;
  }

  public int getRestSecsLeft() {
    return restSecsLeft;
  }

  public void setRestSecsLeft(int restSecsLeft) {
    this.restSecsLeft = restSecsLeft;
  }

  public boolean getPrevSetOver() {
    return prevSetOver;
  }

  public void setPrevSetOver(boolean prevSetOver) {
    this.prevSetOver = prevSetOver;
  }

  public boolean getWorkoutOver() {
    return workoutOver;
  }

  public void setWorkoutOver(boolean workoutOver) {
    this.workoutOver = workoutOver;
  }

}

I have a service that runs after a workout is created, and after debugging odd behavior, have found an instance where the value of prevSetOver that is saved in a workout RealmObject is different than the value returned from w.getPrevSetOver(). I am not sure how this is happening--I do not change the value of the variable prevSetOver after an object is instantiated. I am a new realm user and do not understand how this is happening. The picture I have attatched is a screenshot of the w.prevSetOver() method and the RealmObject having different values.

There are more variables in the debugger in this screen, I left most of them out in my post for simplicity's sake.

enter image description here

Upvotes: 0

Views: 1375

Answers (2)

marlonpya
marlonpya

Reputation: 624

realm.beginTransaction();
Workout w = realm.createObject(Workout.class);
w.setmWorkoutId(UUID.randomUUID().toString());
realm.copyToRealm(w); //<-- u need
realm.commitTransaction();

is better add Primarykey, changed u String variable to long

@PrimaryKey
private long mWorkoutId;

Upvotes: 0

beeender
beeender

Reputation: 3575

It is the right behaviour of Realm.

Realm generates Proxy object which inherit from your Workout when compiling. And read/write data from/to Realm is actually implemented by the Proxy Object through overriding getters/setters. The original Object's member field won't be changed by Realm.

When Realm.createObject() get called, it does return a Proxy object, whose member fields are not what you expected.

You still can create a instance of the original model object which we call it standalone object (means it is not managed by Realm) by calling Workout w = new Workout(). This would act just like normal Java object. And you still can copy it to Realm by calling w = realm.copyToRealmOrUpdate(w). Notice we changed the w's value to the return value. The function will return a Proxy object which is managed by Realm now.

Upvotes: 2

Related Questions