5

So I have this data object, that has a set of strings (representing non-indexable keywords but that doesn't matter here) as a member and Room does not behave well when this set is empty:

@Parcelize
@Entity(tableName = TABLE_NAME)
data class DataItem(
        @PrimaryKey
        @ColumnInfo(name = COLUMN_ID, index = true) val id: Long,
        @ColumnInfo(name = COLUMN_GROUP, index = true) var group: Int,
        @ColumnInfo(name = COLUMN_TEXT, index = true) var text: String,
        @ColumnInfo(name = COLUMN_STRING_SET) var stringSet: Set<String>
): Parcelable {

    companion object
    {
        const val TABLE_NAME = "TestDataItems"
        const val COLUMN_ID = "id"
        const val COLUMN_GROUP = "groupColumn"
        const val COLUMN_TEXT = "text"
        const val COLUMN_STRING_SET = "stringSet"
    }
}

First, I get an error because room doesn't know how to handle this data. Fair enough, let's implement a type converter. Keeping it simple without a JSON conversion, since I know \n will be a valid delimiter.

class TestTypeConverter {

    @TypeConverter
    fun fromStringSet(value: Set<String>): String {
        return value.joinToString ( "\n" )
    }

    @TypeConverter
    fun toStringSet(value: String): Set<String> {
        return value.split("\n").toSet()
    }
}

Room accepts my humble offer of type conversion and compiles a nice dao implementation from my defined interface.

@androidx.room.Dao
@TypeConverters(value = [TestTypeConverter::class])
interface TestDao{

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertItem(item: DataItem)

    @Query("SELECT * FROM $TABLE_NAME")
    fun getAllItems() : LiveData<List<DataItem?>>

    @Update
    fun updateItem(item: DataItem)

    @Delete
    fun deleteItem(item: DataItem)

    @Query("UPDATE $TABLE_NAME SET $COLUMN_STRING_SET= :stringSet WHERE $COLUMN_ID= :id")
    fun updateStringSet(id: Long, stringSet: Set<String>)
}

During testing I did find though, that there is a problem under the hood. Compare the 2 different implementations for type conversion once in the update adapter (which takes a complete data object as input, and once in a custom query targeted at only updating the string set:

this.__updateAdapterOfDataItem = new EntityDeletionOrUpdateAdapter<DataItem>(__db) {
      @Override
      public String createQuery() {
        return "UPDATE OR ABORT `TestDataItems` SET `id` = ?,`groupColumn` = ?,`text` = ?,`stringSet` = ? WHERE `id` = ?";
      }

      @Override
      public void bind(SupportSQLiteStatement stmt, DataItem value) {
        stmt.bindLong(1, value.getId());
        stmt.bindLong(2, value.getGroup());
        if (value.getText() == null) {
          stmt.bindNull(3);
        } else {
          stmt.bindString(3, value.getText());
        }
        final String _tmp;
        _tmp = __testTypeConverter.fromStringSet(value.getStringSet()); // <-- Room uses my type converter
        if (_tmp == null) {
          stmt.bindNull(4);
        } else {
          stmt.bindString(4, _tmp);
        }
        stmt.bindLong(5, value.getId());
      }
    };
  }


@Override
  public void updateStringSet(final long id, final Set<String> stringSet) {
    __db.assertNotSuspendingTransaction();
    StringBuilder _stringBuilder = StringUtil.newStringBuilder();
    _stringBuilder.append("UPDATE TestDataItems SET stringSet= ");
    final int _inputSize = stringSet.size();
    StringUtil.appendPlaceholders(_stringBuilder, _inputSize);
    _stringBuilder.append(" WHERE id= ");
    _stringBuilder.append("?");
    final String _sql = _stringBuilder.toString();
    final SupportSQLiteStatement _stmt = __db.compileStatement(_sql);
    int _argIndex = 1;
    for (String _item : stringSet) {                           // <-- Room IGNORES the type converter
      if (_item == null) {
        _stmt.bindNull(_argIndex);
      } else {
        _stmt.bindString(_argIndex, _item);
      }
      _argIndex ++;
    }
    _argIndex = 1 + _inputSize;
    _stmt.bindLong(_argIndex, id);
    __db.beginTransaction();
    try {
      _stmt.executeUpdateDelete();
      __db.setTransactionSuccessful();
    } finally {
      __db.endTransaction();
    }
  }

So in the custom query room doesn't use the provided type converter and instead treats the set as a list and wants to add the string values comma-separated. This causes some obvious problems:

  • My type converter uses a different delimiter, so the queries are not compatible!
  • If the set is empty, the SQL string is not valid

Both causes runtime failures, and preventing those is supposed to be the whole point of room!

So the question is: am I doing something wrong? I reported it as a Bug to Google, but haven't received any response yet.

Possibly related questions here and here and here

ThePMO
  • 413
  • 4
  • 13
  • It doesn't matter to what I convert it (I tried both). Actually using a JSON converter still ends up being stored as a JSON string in the DB. The point is that Room ignores the TypeConverter, no matter what's in there. – ThePMO Mar 04 '21 at 11:10

1 Answers1

0

Just use String:

@Query("UPDATE $TABLE_NAME SET $COLUMN_STRING_SET= :string WHERE $COLUMN_ID= :id")
fun updateStringSet(id: Long, string: String)
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Sure, that could work in this case, but it's just a workaround. It's a bit disappointing to have to do something like this, I does look like it should work out of the box. – ThePMO Mar 10 '21 at 18:28