I have two models -
class Direction : RealmObject {
var distance : Int
var polyline : String
}
class Route : RealmObject {
var id : String
var directionList : RealmList<Direction>
}
I have been using insertOrUpdate() to update Route class with the assumption that when I call it, the existing direction objects in directionList were removed and replaced with the new list I provided. However, I recently discovered that that is not happening. Even cascade delete is not supported when I call route.deleteFromRealm(). So now I've ended up with hundreds of objects in Direction table with no objects referring to them.
How can I remove all those objects from Direction class which have no Route objects referring to them in Realm migration?
Two possible ways I can think of.
The first may not help you at this time, but it can in future perhaps. By adding a LinkingObjects property to the
Directionclass, you can let the model determine whichDirectionobjects have no relatedRouteobjects. LinkingObjects is described here (https://realm.io/docs/java/5.8.0/api/io/realm/annotations/LinkingObjects.html). With a property onDirection: E.g.:Then you can delete any objects where:
You may need to do this for your next release though.
A second way is more long winded, but essentially:
Directionobjects:RealmResults<Direction> redundantDirections = realm.where(Direction.class).findAll();Routeobjects (similar to above).Routeobjects.redundantDirectionsquery to exclude anyDirectionobjects referenced by eachRouteobject.redundantDirections.I'd hope there's a third way I'm unaware of.....