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

2

10

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).

Suppuration answered 1/1, 2019 at 21:5 Comment(2)
The release notes have: "Additional Rx Return Types Support: DAO methods annotated with @Insert, @Delete or @Update now support Rx return types Completable, Single<T> and Maybe<T>". No mention of a DELETE in a @Query, though.Acidhead
I think you can't use it like that you need to create it from call back like this Single.fromCallable and make the deleteAll returns intMana
A
7

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).

Alitta answered 1/1, 2019 at 21:24 Comment(0)
M
5

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.

Mentalism answered 20/11, 2019 at 21:45 Comment(3)
In case room-rxjava2 2.2.1 stable fixes this, this should be current answer to the problem. It's the library which provides the bindings, so it might know about Completable.Alitta
@MartinZeitler It knows, I just did it yesterday. They updated the libraries. I first used your response, then I saw that there are updates on the libraries and I saw it works. This is why I added the comment. Thanks for your answer :)Mentalism
Yeah, when I get this error, first thing I look is my build.gradle project file.Jugate

© 2022 - 2024 — McMap. All rights reserved.