So far for storing in Room Database I've been using type converter for each classes. Like this:
@SerializedName("sidebar")
@Expose
@TypeConverters(SidebarConverter.class)
private Sidebar sidebar;
@SerializedName("splash")
@Expose
@TypeConverters(SplashConverter.class)
private Splash splash;
@SerializedName("overview")
@Expose
@TypeConverters(OverviewConverter.class)
private Overview overview;
@SerializedName("home")
@Expose
@TypeConverters(HomeConverter.class)
private Home home;
@SerializedName("portfolio")
@Expose
@TypeConverters(PortfolioConverter.class)
private Portfolio portfolio;
@SerializedName("team")
@Expose
@TypeConverters(TeamConverter.class)
private Team team;
I want to know if there's a more convenient way to use one TypeConverter
single handedly in Database.
The issue here is that Room's code generation tries to find specific type, and if you try to make a generic converter it fails to generate appropriate methods. However, if in your case it's appropriate to transfrom data to json for storage, you can reduce boilerplate like this:
fromJson and toJson are generic, for example they can look like this. Any time you need to add types, just take two methods above and replace 'Something' with your type. If you have a lot of classes to convert, you can even code-gen TypeConverters like this pretty easily to satisfy Room's code-gen needs.
You can define all your converter in a Single Class like this:
And then set this converter on your Room Database with
@TypeConverter
annotation like this which work globally on any@Entity
class.You don't need to define@TypeConverter
Individually in Entity classNote we’ve added a new annotation named
@TypeConverters
in our database definition in order to reference the different converters that we can have (you can separate it by commas and add others).