Reputation: 3457
I am currently trying to add a RealmObject to RealmList inside another RealmObject.
So this is the way I am doing it at the moment.
First I create and save a RealmObject called "RouteRealm" like this:
public void insertNewRoute(int routeId, long routeDate) {
realm.beginTransaction();
RouteRealm routeRealm = realm.createObject(RouteRealm.class);
routeRealm.setId(routeId);
routeRealm.setDate(routeDate);
realm.commitTransaction();
}
The class RealmObject looks like this:
public class RouteRealm extends RealmObject {
@PrimaryKey
private int id;
private long date;
private RealmList<RoutePointRealm> routePoints;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public RealmList<RoutePointRealm> getRoutePoints() {
return routePoints;
}
public void setRoutePoints(RealmList<RoutePointRealm> routePoints) {
this.routePoints = routePoints;
}
}
The above works. The problem occurs when I try to add a RoutePointRealm to the list called routePoints. Here is my code for adding the RoutePointRealm object to the list:
public void insertNewRoutePoint(int routeId, String address, float latitude, float longitude, long routePointId, long routePointTime) {
realm.beginTransaction();
RouteRealm routeRealm = realm.where(RouteRealm.class).equalTo("id", routeId).findFirst();
RoutePointRealm routePointRealm = realm.createObject(RoutePointRealm.class);
routePointRealm.setAddress(address);
routePointRealm.setLatitude(latitude);
routePointRealm.setLongitude(longitude);
routePointRealm.setRoutePointID(routePointId);
routePointRealm.setRoutepointTime(routePointTime);
routeRealm.getRoutePoints().add(routePointRealm);
realm.copyToRealmOrUpdate(routeRealm);
realm.commitTransaction();
}
And the RoutePointRealm looks like this:
public class RoutePointRealm extends RealmObject {
@PrimaryKey
private long routePointID;
private float longitude, latitude;
private long routepointTime;
private String address;
public long getRoutePointID() {
return routePointID;
}
public void setRoutePointID(long routePointID) {
this.routePointID = routePointID;
}
public float getLongitude() {
return longitude;
}
public void setLongitude(float longitude) {
this.longitude = longitude;
}
public float getLatitude() {
return latitude;
}
public void setLatitude(float latitude) {
this.latitude = latitude;
}
public long getRoutepointTime() {
return routepointTime;
}
public void setRoutepointTime(long routepointTime) {
this.routepointTime = routepointTime;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
For some reason, the RoutePointRealm are added to the list, but all of its fields are set to zero and null. I have debugged my app to make sure that all of the parameters contains the correct values with the right datatypes etc. So now I know that the problem is related to the Realm methods.
What am I doing wrong?
Upvotes: 0
Views: 3509
Reputation: 81539
My personal recommendation:
public void insertNewRoute(final int routeId, final long routeDate) {
final RouteRealm routeRealm = new RouteRealm();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
routeRealm.setId(routeId);
routeRealm.setDate(routeDate);
realm.insertOrUpdate(routeRealm);
}
});
}
public void insertNewRoutePoint(final int routeId, final String address, final float latitude,
final float longitude, final long routePointId, final long routePointTime) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RouteRealm routeRealm = realm.where(RouteRealm.class).equalTo(RouteRealmFields.ID, routeId).findFirst();
RoutePointRealm routePointRealm = new RoutePointRealm();
routePointRealm.setAddress(address);
routePointRealm.setLatitude(latitude);
routePointRealm.setLongitude(longitude);
routePointRealm.setRoutePointID(routePointId);
routePointRealm.setRoutepointTime(routePointTime);
routePointRealm = realm.copyToRealmOrUpdate(routePointRealm);
routeRealm.getRoutePoints().add(routePointRealm);
}
});
}
Upvotes: 0
Reputation: 3457
First of all, thank you for your answers! I still couldn't get it to work after changing the solutions you've proposed. At least I didn't think so.
The reason I thought it didn't work, was partly because of a mistake that I made with my gui showing a zero value.. This made me to go into debugging the app, but apparently the debugger always shows zero or null values for the Realm objects.. At least in my case. So at last, I tried making a Toast message with a fetched value from Realm and it returned what it was supposed to.
So I don't think that there were any problems to begin with.. The debugger just got me thinking so. I am sorry if I wasted your time, but I still thought that I should post this as an answer if other were to encounter the same problem.
Upvotes: 1
Reputation: 1009
the problem comes from ID, Realm not support auto increment behaviour so you should do it manually. something like :
beginTransaction()
foo.setId(value)
commitTrasaction()
Upvotes: 0
Reputation: 2497
All your objects are managed by Realm, so you don't need the realm.copyToRealmOrUpdate(routeRealm);
call.
Upvotes: 0