Room RxJava Flowable not emitting updates after insert

Viewed 1827

I have a Service that inserts data into a Room database everytime a new location is found.

@Override
public void onLocationUpdate(Location location) {
    final Point point = new Point();
    point.setTripId(0);
    point.setDate(new Date());
    point.setLatitude(location.getLatitude());
    point.setLongitude(location.getLongitude());

    Completable completable = Completable
            .fromAction(() -> appDataBase.pointDao().insert(point));
}

But for some reason, when I subscribe in the main Activity, I'm not getting the new values in the database, it returns an empty list only once:

appDataBase.pointDao().getAllByTripId(0)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(points -> Log.d("TripActivity", points.toString()));

Here's my Dao:

@Dao
public interface PointDao {
    @Query("SELECT * FROM points WHERE tripId = :tripId")
    Flowable<List<Point>> getAllByTripId(int tripId);

    @Insert
    void insert(Point point);

    @Query("DELETE FROM points WHERE tripId = :tripId")
    void deleteByTripId(int tripId);
}

My Entity:

@Entity(tableName = "points")
public class Point {

    public static final int DEFAULT_TRIP_ID = 0;

    @PrimaryKey(autoGenerate = true)
    private int id;
    private int tripId;
    private double latitude;
    private double longitude;
    private Date date;

    public Point() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getTripId() {
        return tripId;
    }

    public void setTripId(int tripId) {
        this.tripId = tripId;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

RoomDatabase:

@Database(entities = {Point.class, Trip.class}, version = 1, exportSchema = false)
@TypeConverters({Converters.class})
public abstract class AppDataBase extends RoomDatabase {
    public abstract TripDao tripDao();
    public abstract PointDao pointDao();
}

This is the database construction which I inject with Dagger.

    @Provides
    @Singleton
    public AppDataBase provideAppDataBase(Context context) {
        return Room.databaseBuilder(context, AppDataBase.class, "Trips").build();
    }

I really don't know what I'm missing.

2 Answers

It was a Dagger problem. I was using two different appDataBase instances and that's why flowable wasn't reacting to insertions. I feel like an idiot, but hope this helps others in the same situation.

Completable completable = Completable
        .fromAction(() -> appDataBase.pointDao().insert(point));

You have to subscribe to your completable for it begin it's work.

Related