So i have an interactor which performs an insert operation with Realm and then notifies that the insertion is completed with a RealChangeListener. It is something like this:
fun insertCar(item: Car) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(...)
}
}
I can do it this way:
fun insertCar(item: Car, listener: RealmChangeListener<Car>) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(listener)
}
}
And access like this:
realmInteractor.insertCar(item, RealmChangeListener {
// do something here
})
But then i have no way of removing this listener
realmInteractor.insertCar(item, RealmChangeListener {
// do something here
it.removeChangeListener(this)
})
this
will point to the class its located not the actual listener
I can also do this:
fun insertCar(item: Car, doAfterChange (Car) -> Unit) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(RealmChangeListener{
doAfterChange()
})
}
}
But then i have a SAM inside another SAM (too overkill imo)
I can do it like this:
fun insertCar(item: Car, listener: RealmChangeListener<Car>) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(listener)
}
}
realmInteractor.insertCar(item, object : RealmChangeListener<Car> {
override fun onChange(element: Car?) {
...
element?.removeChangeListener(this)
}
})
Which works but it is too verbose.
So how do you deal with this and what is considered the best approach?