I have nested items in my database like e.g. house - rooms. If I delete a house I also want to delete all rooms, but I do not want to think about this, so the house must know this. So my setup would look something like this:
fun deleteHouse(item: House): Single<House> {
houseObservable // emits List<House>
.take(1)
.map {
DBManager.beginTransaction()
it
}
.flatMapIterable { it }
.flatMapSingle { deleteRoom(it) }
.toList()
.map {
DBManager.deleteWithoutDependencies(item)
DBManager.endTransaction()
item
}
}
fun deleteRoom(item: Room): Single<Room> {
roomObservables // emits List<Room>
.take(1)
.map {
DBManager.beginTransaction()
it
}
.flatMapIterable { it }
.flatMapSingle { DBManager.deleteWithoutDependencies(item) }
.toList()
.map {
DBManager.endTransaction()
item
}
}
Problem
Every item is deleted on RxComputationThread-1
and every beginTransaction
and endTransaction
as well, BUT the last endTransaction
that finished the deletion of the house.
I need to run all database actions on the same thread, otherwise the database will lock itself out...
Any ideas how to solve this?
Idea
Pass the used Schedular
to the functions and use the same in the flatMaps
, but is this really necessary to solve this problem?