Hi everyone! I have an interface that is implement...
# android
n
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
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
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
gotcha
z
Better way to do this is by not doing it. This is abstraction without any benefits. There’s added complexity as well