Luke Scales
Luke Scales

Reputation: 99

Applying TypeConverters to Dao Android Room

I'm using a type converter to handle Date objects in my Room database. I'm wondering if there is any difference between annotating the entire Dao with the type converter like so:

//TypeConverter for all
@Dao
@TypeConverters(DateConverter.class)
public interface MealDao {
...

or only annotating the methods that need the type converter, like:

//This method needs TypeConverter
@TypeConverters(DateConverter.class)
@Query("SELECT * From Meal " +
        "WHERE Meal.mealTime > :dayStart " +
        "AND Meal.mealTime < :dayEnd")
List<Meal> findAllMealsByDay(Date dayStart, Date dayEnd);

//This method doesn't
@Query("SELECT Meal.mealType From Meal " +
        "WHERE Meal.id = :mealId ")
int retrieveMealType(long mealId);

etc.? 4 out of 8 methods in this Dao require the TypeConverter. Would declaring for entire Dao affect performance of methods that do not need it or anything else I should be aware of?

Upvotes: 3

Views: 1419

Answers (1)

lomza
lomza

Reputation: 9716

As it is written in the documentation: " The TypeConverter is added to the scope of the element so if you put it on a class / interface, all methods / fields in that class will be able to use the converters.

If you put it on a Dao, all methods in the Dao will be able to use it.

If you put it on a Dao method, all parameters of the method will be able to use it.

If you put it on a Dao method parameter, just that field will be able to use it."

Add TypeConverter to the least scope you need - annotate one method if you need it there only, or on a whole Dao class if it's used in a few methods, or - on a whole DB, if it's widely used.

Upvotes: 1

Related Questions