Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I update a TypeConverted column of an entity in Room Dao function

I have an @Entity which holds a variable(list of custom object) along with other fields for the table. I am able to insert, fetch and delete from this entity.

But I am facing an issue in updating the entity:

I want to update that particular field which holds a list of custom object in the table but while compilation it throws an error:

error: Query method parameters should either be a type that can be converted into a
database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.

I could update the complete row object but the problem lies in updating this single field. I am using TypeConverters on my @Database class but I have tried using them on Dao and the update function itself but it reports the same error.

Can someone please help me to update this particular field in the row, I don't want to provide the full object of this entity for this to happen.

My Entity is:

@Entity data class TableName(
    @PrimaryKey
    var id: String = "",
    @SerializedName("varOne")
    @Expose
    var varOne: List<CustomObjects>? = null)

Update method is something like this:

@TypeConverters(MyTypeConverters.VarOneListTypeConverters::class)
@Query("Update TableName SET varOne = :varOneList")
abstract fun updateTableName(varOneList: List<CustomObjects>)
like image 658
skygeek Avatar asked Oct 05 '18 09:10

skygeek


People also ask

How to delete data from Room database in Android?

3.1 Add the Clear all data menu option In the Options menu, select Clear all data. All words should disappear. Restart the app. (Restart it from your device or the emulator; don't run it again from Android Studio) You should see the initial set of words.

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.


1 Answers

Ideally you should try to model this as a relationship with a separate table for the CustomObject and a foreign key ref to the primary key of TableName. However you can still write a converter for the type List<CustomObject>. Room only understands the Sqlite Data types and any other type needs to be converted to one of they types that room understands. They have provided the TypeConverter annotations for the same. If you use Gson to serialize your CustomObject then you can use the following converter. The code is self explanatory

public class Converters {
   @TypeConverter
   public static ArrayList<String> fromString(String value) {
      Type listType = new TypeToken<ArrayList<CustomObject>>() {}.getType();
      return new Gson().fromJson(value, listType);
   }
   @TypeConverter
   public static String fromArrayList(ArrayList<CustomObject> list) {
      Gson gson = new Gson();
      String json = gson.toJson(list);
      return json;
   }
}

And you just have to add this converter to your Database class

@TypeConverters(Converters::class) 
abstract class YourDatabase extends RoomDatabase
like image 191
paradox Avatar answered Sep 19 '22 15:09

paradox