Intellij IDEA generates update method from another object instance. (update RealmObject)

I have a normal POJO similar to the following:

public class Car extends RealmObject{

    private String name;
    private int maxSpeed;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }
}

      

Is there a way to create an update / copy method that another instance of the car gets? Something like that:

public void update(Car other){
    setName(other.getName());
    setMaxSpeed(other.getMaxSpeed());
}

      

I am getting a car in JSON format from a server and I am using GSON to get an instance of Car. If I want to keep the car in the kingdom, I have to do something like this:

Car receivedCar = getCarFromServer();
Car car = realm.createObject(Car.class);
car.setName(receivedCar.getName());
car.setMaxSpeed(receivedCar.getMaxSpeed());

      

Please note that due to Realm I cannot use the copy constructor.

My real models have over 25 fields so this will be a killer job. I would rather create an update method and do something like this:

Car receivedCar = getCarFromServer();
Car car = realm.createObject(Car.class);
car.update(receivedCar);

      

Does anyone know how I can simplify my job?

+3


source to share


1 answer


Kingdom Christian is here. We are currently exploring how to make the JSON API easy to use. Our current work is in the process of being searched: https://github.com/realm/realm-java/pull/489 . Which basically does what you want as a 1-liner.



It's not in the main repository yet, so until then, you need to either manually create the Realm or create a copy method like the one you described. Feedback is highly appreciated though.

+1


source







All Articles