Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a normal POJO like 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 generate an update/copy method that receives another instance of Car? Something like this:

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

I receive a car in JSON format from a server and I use GSON to get a Car instance. If I want to save the car in a Realm I have to do something like this:

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

Note that because of Realm I can't use a copy constructor.

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

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

Does anyone have any idea how I can make my job easier?

like image 708
Tudor Luca Avatar asked Feb 04 '26 23:02

Tudor Luca


1 Answers

Christian from Realm here. We are currently looking into how to make an easy to use JSON API. Our current work in progress can be found in this pull request: https://github.com/realm/realm-java/pull/489. Which basically does what you want as a 1-liner.

It is not in the main repository yet, so until then you either have to build Realm manually or create a copy method like the one you described. Feedback is very welcome though.

like image 147
Christian Melchior Avatar answered Feb 06 '26 12:02

Christian Melchior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!