Reputation: 1731
I'm new to RxJava and I wonder what is the best way to integrating RxJava with android room. I've seen two ways to do that:
1.
@Dao
interface UserDao{
@Insert
void insert(User user);
}
class Repository {
public Completable <Boolean> insertUser(final User user) {
return Completable.fromCallable(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
mAppDatabase.userDao().insert(user);
return true;
}
});
}
}
2.
@Dao
interface UserDao{
@Insert
Completable insert(User user);
}
So is there any difference between above codes?Which one is a better approach?
Upvotes: 0
Views: 137
Reputation: 405
Room supports RxJava2. You will probably need to add dependency android.arch.persistence.room:rxjava2:$version
.
Moreover, starting with Room 2.1.0-alpha01, DAO methods annotated with @Insert, @Delete or @Update support Rx return types Completable.
I recommend you to take a look to the following article where you can find detailed instruction - Room 🔗 RxJava.
As the result your DAO will look like the following and there will be no need to transform to rx type inside repository:
@Dao
interface UserDao{
@Insert
Completable insert(User user);
}
}
Upvotes: 1