Fellipe Peixoto
Fellipe Peixoto

Reputation: 35

RXJava "Single<Long> insert" only insert correctly before the activity is finished ( finish() )

I having some trouble using RXJava with Room. I trying to insert some data and after the insert (onSucess) call finish() to close the activity.

Everything is okay if I don't call finish() on the activity, I insert how much data I want, the ids returned are right (1, 2, 3, 4...) and data are displayed on table. If a call finish() and open the activity again, onSucess(Long aLong) only returns id 1 and the table is not uptaded anymore.

There is the code:

Client client = new Client();
client.birth_date = birthDate.toString();
client.health_status = healthStatus;
client.lives_with = livesWith;
client.name = name;
client.school_time = schoolTime;

singleObserver = database().clientDAO().insert(client);
singleObserver.subscribeOn(Schedulers.computation())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new DisposableSingleObserver<Long>() {
        @Override
        public void onSuccess(Long aLong) {
            String formated = String.format(getApplication().getString(R.string.new_client_register), client.name);
            Toast toast = Toast.makeText(getApplication(), formated, Toast.LENGTH_SHORT);
            toast.show();
            finish();
        }

        @Override
        public void onError(Throwable e) {

        }
});

In DAO I just declare:

@Insert
Single<Long> insert(Client client);

I'm stuck in that for 2 days, and I don't find out what I'm doing wrong. And the question is, why Single behaviour is different after I finish the activity. I have tried to unsubscribe and searched somehow to destroy the variable attached to it, but the behaviour still the same.

Someone can help me?

Upvotes: 0

Views: 86

Answers (1)

Fellipe Peixoto
Fellipe Peixoto

Reputation: 35

There is no problem with my RXJava code.

I figured out what happened when I oppened the DataBase inspector. Two instances of database are created, because I call Room.inMemoryDatabaseBuilder each time I open the activity.

I just forgot to call the singleton to keep a unique instance (only when developing the app)

If someone needs, thats is the way I use to keep the final database clean:

public static synchronized Database getInstance(Context context) {
        if (instance == null && !BuildConfig.DEBUG) {
            instance = Room.databaseBuilder(context.getApplicationContext(),
                    Database.class,
                    DBNAME)
                    .allowMainThreadQueries()
                    // TODO: MIGRATE METHOD
                    // TODO: REMOVE BEFORE LAUNCH FOR FINAL USER, DATA LOSS DANGER!
                    .fallbackToDestructiveMigration()
                    .build();
        }

        // TODO: change build to release when launch
        if (instance == null && BuildConfig.DEBUG) {
            instance =Room.inMemoryDatabaseBuilder(context.getApplicationContext(),
                    Database.class)
                    .allowMainThreadQueries()
                    .build();
        }

    return instance;
}

Note: In my case allowMainThreadQueries() is not dangerous, regardless I don't recommend haha.

Upvotes: 0

Related Questions