user3903940
user3903940

Reputation:

Android Room Database Table is not updating

I am trying to update a value from my Table using RX2 and room.

My item is

@Entity(tableName = "myCustomTable")
public class MyItem  {

    @NonNull
    @PrimaryKey
    @ColumnInfo(name = "item_id")
    @SerializedName("item_id")
    private Long item_id;

    @ColumnInfo(name = "name")
    @SerializedName("name")
    private String name;

    @ColumnInfo(name = "country")
    @SerializedName("country")
    private String value;

    @SerializedName("image")
    @ColumnInfo(name = "image")
    private String image;

    @ColumnInfo(name = "latitude")
    @SerializedName("latitude")
    private Double latitude;

    @ColumnInfo(name = "longitude")
    @SerializedName("longitude")
    private Double longitude;

    @ColumnInfo(name = "valid")
    private boolean valid;

    public MyItem (Long item_id, String name, String value, String image, Double latitude, Double longitude, boolean valid) {
        this.item_id = item_id;
        this.name = name;
        this.value = value;
        this.image = image;
        this.latitude = latitude;
        this.longitude = longitude;
        this.valid = true;
    }

    public boolean isValid() {
        return valid;
    }

    public Long getItem_id() {
        return item_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Double getLatitude() {
        return latitude;
    }

    public Double getLongitude() {
        return longitude;
    }
}

My query is this:

@Dao
public interface CustomDao {
  @Insert(onConflict = OnConflictStrategy.REPLACE)
  void insertItem(MyItem item);

  @Query("UPDATE myCustomTable SET valid = :isValid WHERE item_id= :id")
  void flagQueued(long id, boolean isValid);
}

By default the valid is true;

This will be called and converted to a completable by the database implementation

public class LocalDataSource implements DataSource {
  private CustomDao customDao;

  @Inject
  public LocalPupilDataSource(CustomDao customDao) {
    this.customDao = customDao;
  }

  public Completable flagQueued(long id) {
    return Completable.fromAction(() -> customDao.flagQueued(id,false));
  }
}

And eventually, I call the flagQueued method by a subscriber. This subscriber is responding with a successful completion.

mCompositeDisposable.add(viewModel.deleteItem(id)
  .subscribeOn(schedulerProvider.computation())
  .observeOn(schedulerProvider.ui())
  .subscribe(() -> Snackbar.make(getActivity().findViewById(R.id.main_layout),
    R.string.user_removed, Snackbar.LENGTH_SHORT).show(), error -> Timber.e(error,error.getMessage()) ) );

Unfortunately, the value is not being updated. Of course, the ID exists in the table.

Any advice would be much appreciated.

Upvotes: 4

Views: 3057

Answers (1)

user3903940
user3903940

Reputation:

Found the problem. After hours of debugging.

The default value I assign on the constructor this.valid = true; for some reason broke the QUERY.

So the solution is just to remove the fault value, and assign my desired value to the Item when I am about to insert it into the table.

Is there any GitHub repo for room where I could post the issue ?

Upvotes: 3

Related Questions