From the documentation from Google:
// Returns the number of users inserted.
@Insert(onConflict = OnConflictStrategy.REPLACE)
public ListenableFuture<Integer> insertUsers(List<User> users);
This is my code:
@Insert(onConflict = OnConflictStrategy.REPLACE)
ListenableFuture<Integer> insert(List<VacationDay> vacationDay);
which yields this exception:
error: Not sure how to handle insert method's return type.
Why? How to fix it?
EDIT:
These are my room related dependencies:
implementation 'androidx.room:room-runtime:2.5.1'
annotationProcessor 'androidx.room:room-compiler:2.5.1'
implementation "androidx.room:room-guava:2.5.1"
As MikeT's comment pointed out, Long instead of Integer works:
ListenableFuture<Long> insert(List<VacationDay> vacationDay);
That is, unlike the @Delete
and @Update
convenience annotations that return an int/Integer value, that indicates the number of affected rows (0 or greater).
@Insert
returns a long/Long per inserted row (Room will handle multiple inserts by returning an array of longs).
If a row was not inserted due to a captured conflict (e.g. a UNIQUE constraint conflict, that has been ignored by the OnConflictStrategy
) then it will return -1 for the row that was not inserted.
The reason for a long/Long is that when inserting you are typically interested in each row which may or may not have been inserted. The value returned is the rowid of the inserted row (which may or may not be a known/coded/utilised column) and that it's value is a 64 bit signed integer value i.e. potentially too large to be handled by an int/Integer and hence why a long/Long is returned.
The rowid is a column that virtually all tables in Room have. If the Primary Key column is solely an integer type then it will be an alias of the rowid column and thus be the same value as the rowid column.
In regards to the comment So the official documentation from Google is bogus to the point that it doesn't even compile?!
Clearly the documentation is incorrect and is likely just a copy and paste and then amendment of the delete or update examples.