Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room how can I convert a custom made object

I'm new to Room, and I'm not understanding how I should go about this. I have an Entity Movie and another Entity called ÙpcomingMovies.

    @Entity
    public class Movie {

        @PrimaryKey
        @NonNull
        public String id;

        @ColumnInfo
        public String name;

        @ColumnInfo
        public String title;
    }

    @Entity
    public class UpcomingMovies {

        @PrimaryKey(autoGenerate = true)
        public int id;

        @ColumnInfo
        public Movie movie;
    }

So I already know Room has a problem with converting Objects, but I still havent seen how to convert a custom one with TypeConverter. Probably I'm complicating something, but can someone give me a hand tackling this issue ? I'm not even sure if my UpcomingMovies table is well made.

Appreciate it

like image 206
Greggz Avatar asked Aug 24 '18 17:08

Greggz


People also ask

What is TypeConverter in Android?

Use type converters You support custom types by providing type converters, which are methods that tell Room how to convert custom types to and from known types that Room can persist. You identify type converters by using the @TypeConverter annotation.

What is room DB Android?

Room is a persistence library that provides an abstraction layer over the SQLite database to allow a more robust database. With the help of room, we can easily create the database and perform CRUD operations very easily.

What is embedded in room database?

room. Embedded instead. Can be used as an annotation on a field of an Entity or Pojo to signal that nested fields (i.e. fields of the annotated field's class) can be referenced directly in the SQL queries. If the container is an Entity , these sub fields will be columns in the Entity 's database table.

What is room database in Android Kotlin?

Room allows you to create tables via an Entity. Let's do this now. Create a new Kotlin class file called Word containing the Word data class. This class will describe the Entity (which represents the SQLite table) for your words. Each property in the class represents a column in the table.


1 Answers

What you need to do is tell Room how to convert your class into a type that it knows how to store, in most cases this can be a String representation.

Firstly create a class for your TypeConverters and within it declare a function that can convert your type to and from the type you want Room to store it as. Don't forget to annotate these functions.

class MyTypeConverters {

@TypeConverter
fun appToString(app: App): String = Gson().toJson(app)

@TypeConverter
fun stringToApp(string: String): App = Gson().fromJson(string, App::class.java)

}

Then all you need to do is tell Room about your TypeConverters when you declare your database

@TypeConverters(MyTypeConverters::class)
abstract class AppDatabase : RoomDatabase() {
..DAO declarations
}

That's it.

like image 172
Ivan Wooll Avatar answered Oct 04 '22 15:10

Ivan Wooll