Android Room throws error: Deletion methods must either return void or return int (the number of deleted rows)

Viewed 6880

I'm using Android Room with RxJava

dependencies {
    implementation 'androidx.room:room-rxjava2:2.1.0-alpha02'
}

I need to get Completable from parameterized deletion methods, I thought this feature is added as of 2.1.0? ex.

  @Query("DELETE FROM message_table WHERE uid = :id")
  Completable delete(String id);

  @Query("DELETE FROM message_table")
  Completable deleteAll();

Still throws error: Deletion methods must either return void or return int (the number of deleted rows).

2 Answers

As the error message is trying to tell:

With @Query, you have to change the returned data-type from Completable to int or void.

The Completable would need to subscribe to another method, which runs the method of the Dao:

Completable
  .fromAction(aMethodWhichCallsDao)
  .subscribeOn(Schedulers.single())
  .subscribe();

Or use the @Delete annotation, as @Commonsware suggested (in case this works as advertised).

I don't know if you need this anymore, but just went into the same thing today and found out that you need to use all these 3 libs to create DAOs methods to return Completable:

implementation 'androidx.room:room-runtime:2.2.1'
kapt 'androidx.room:room-compiler:2.2.1'
implementation 'androidx.room:room-rxjava2:2.2.1'

I hope this will help you.

Related