Hi everyone! I have an interface that is implemented by a few classes and I have a "List<Interface>" that I have to save in my Room DB.
Every class has its own DAO, but I have a BaseDao too which defines Insert/Update/Delete methods for all the other DAOs. At the moment I'm simply filtering the list with the "filterIsInstance" method to get the class of the objects(I'm doing it for every single class that implements the interface) and then I'm calling the specific class DAO to insert/update/delete the entity.
Is there a better way to do this? In particular, I'm pretty sure that I can avoid filtering the list for every type of class but I don't know how
😶 3
l
Lefko
11/28/2022, 11:05 AM
If you use a sealed interface you can iterate over the list just once.
Copy code
yourList.forEach {
when (it) {
is ConcreteImplementation1 -> ConcreteImplementation1DAO.insert/update/delete(it)
etc
}
}
n
Nikolas Guillen Leon
11/28/2022, 11:13 AM
I'd like to insert the entities in groups and not one by one. I have an Insert query that accepts a list of items and I would prefer to use it instead of the single item insert 🙂
l
Lefko
11/28/2022, 11:14 AM
gotcha
z
Zun
11/28/2022, 3:38 PM
Better way to do this is by not doing it. This is abstraction without any benefits. There’s added complexity as well