Adae Rodríguez
Adae Rodríguez

Reputation: 183

Add new object into relationship in Realm

I have a problem with Realm that ill my brain :(

The problem is when I try to add a new object into RealmList. The object is inserted but it is not linked with the relationship.

Now, my database has multiple relationships:

User 1-->M Trip 1-->M Vehicle 1-->M

And then:

Vehicle 1-->M VehicleInfo

VehicleInfo 1-->M Temperature

The problem reside when I try to insert a new object into Temperature class.

My classes:

Vehicle:

    @RealmClass
    public class Vehicle extends RealmObject
    {
        private String  state;                  //R: Reservado | P: Listo para embarcar | E: Embarcado | P1: Pdte. confirmar medidas | P2: Tª no válida | R1: No embarca
        private String  locata       = "";
        private String  customer     = "";
        private String  customerCode = "";
        private String  originPort   = "";      //Origin port
        private String  destinyPort  = "";      //Destiny port
        private int     fp;                     //Method pay (0 = CASH | 1 = CREDIT)

        //Relationship
        private Trip trip;                              //Inverse
        private RealmList<VehicleInfo>  vehicleInfo = new RealmList<>();    //One-to-many
        private RealmList<Observation> observations = new RealmList<>();;    //One-to-many
.....
}

VehicleInfo:

@RealmClass
public class VehicleInfo extends RealmObject {

    private String  sv;                  //Vehicle type
    private String  licensePlate  = "";  //License
    private String  seal          = "";  //Seal
    private String  temperature   = "";  //Temperature control
    private String  iv            = "";  //Ida/Vuelta
    private String  commodityCode = "";
    private int     tara          = 0;   //TARA
    private int     packages      = 0;   //Bultos
    private int     weight        = 0;
    private double  length        = 0.0; //Meters
    private boolean flagFT;              //Flag Technical data
    private boolean flagDua;
    private boolean flagManifest;
    private boolean flagTransport;
    private boolean flagDangerCommodity;

    //Relationship
    private RealmList<Temperature> temperatures = new RealmList<>();   //One-to-many
....
}

My code to add new Temperature:

Temperature temp = new Temperature();
temp.setDate(appCommon.getCurrentTimeOrDate("DATE"));
temp.setTime(appCommon.getCurrentTimeOrDate("TIME"));
temp.setValue(Double.parseDouble(etTemp.getText().toString()));

VehicleInfoPersistence.updateVehicleInfoTemperature(realm, vehicle.getLocata(), selectedUnitPosition, temp);
updateRecycler(tempRecyclerAdapter, temp);

Method to find and persist in Realm:

public static VehicleInfo findVehicleInfoFromLocata(Realm realm, String locata, int position) {
        RealmQuery<Vehicle> query = realm.where(Vehicle.class).equalTo("locata", locata);
        Vehicle realmVehicle = query.findFirst();

        return realmVehicle.getVehicleInfo().get(position);
    }

public static void updateVehicleInfoTemperature(Realm realm, String locata, int position, Temperature temperature) {

    Vehicle vehicle = VehiclePersistence.findVehicleFromLocata(realm, locata);

realm.beginTransaction();
Temperature realmTemp = realm.copyToRealm(temperature);
 vehicle.getVehicleInfo().get(position).getTemperatures().add(realmTemp);    
realm.commitTransaction();

}

How I said, object is created in database but it is not linked with the vehicle-->vehicleInfo-->Temperature.

What's wrong in my code??

Thanks in advance :)

Upvotes: 0

Views: 2402

Answers (1)

Adae Rodr&#237;guez
Adae Rodr&#237;guez

Reputation: 183

I resolved my problem :)

By some motive, realm fails when I try to add new object into sub-array of a RealmClass.

To solve this issue I've created an intermediate object and then I've added the object to this intermediate object.

public static void updateVehicleInfoTemperature(Realm realm, String locata, int position, Temperature temperature) {

    Vehicle vehicle = VehiclePersistence.findVehicleFromLocata(realm, locata);

    realm.beginTransaction();

    VehicleInfo vInfo            = vehicle.getVehicleInfo().get(position);
    VehicleInfo realmVehicleInfo = realm.copyToRealm(vInfo);
    Temperature realmTemp        = realm.copyToRealm(temperature);
    realmVehicleInfo.getTemperatures().add(realmTemp);

    realm.commitTransaction();
}

I hope to help someone :)

Upvotes: 3

Related Questions