Search code examples
androidlistkotlinmutable

Android Room List or mutable list return type


I was just wondering is there any convention about room queries return type? It is better to return with List or MutableList? It's very easy to convert in Kotlin by .toList() and .toMutableList(). I'm just want to create an eye handy code that is why I'm asking about the best practice.

To See clear, I'm talking about these queries:

@Query("SELECT * FROM measured_attribute WHERE deliveryStatus = :status LIMIT 1000")
fun getAttributeEntityListByStatus(status: DeliveryStatus): List<MeasuredAttributeEntity>

@Query("SELECT * FROM measured_attribute WHERE 
  name = :speific AND 
  fk_patient = :id AND 
  creationTime>:beginTime ORDER BY creationTime")

fun getSpecificAttributeEntity(
   speific: String, 
   id: Long, 
   beginTime: Long
   ): MutableList<MeasuredAttributeEntity>

Solution

  • Room will create a java.util.ArrayList for the results (you can check this by jumping into the implementation of your Dao after building your project), so you can use either return type in this case, as an ArrayList implements both interfaces. In general, the best practice in Kotlin is to use a read-only List as long as you can get away with it, so I'd suggest going with that.