Search code examples
androidrealm

Realm not founding inserted object only after reopening the app


Basically I'm trying to check if an object exists before trying to use it, if not, I'm adding it beforehand then trying to use it.

If it doesn't exist, it gets added correctly but it's still not found by the application afterwards unless I reopen the app

Everything works as intended, however, if the object is there to begin with

private class ExerciseViewPageAdapter extends FragmentPagerAdapter {

    Realm realm;

    ExerciseViewPageAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int pos) {
        realm = Realm.getDefaultInstance();

        String exercise_name = getIntent().getExtras().getString("exercise_name");

        boolean found_exercise = realm.where(Exercise.class).equalTo("name", exercise_name).findAll().size() > 0;
        if (!found_exercise) {
            CompositeDisposable disposable = new CompositeDisposable();
            ExerciseClient client = new ExerciseClient(getApplicationContext());
            disposable.add(client.getExerciseByName(exercise_name)
                    .subscribeOn(Schedulers.io())
                    .subscribe(
                            this::getExercisesSuccess,
                            this::getExercisesError
                    )
            );


        }

        final ArrayList<Exercise> exercise = new ArrayList<>();
        exercise.add(realm.where(Exercise.class).equalTo("name", exercise_name).findFirst());
        if (exercise.get(0) != null) {
            switch (pos) {
//returning new fragments depending on position
            }
        }
//if exercise.get(0) doesn't exist at first, even tho I added it afterwards it's still null here unless I reopen the app or let it crash
        finish();
        realm.close();
        return null;
    }

    private void getExercisesError(Throwable throwable) {

    }


    private void getExercisesSuccess(Exercise exercise) {
        try (Realm r = Realm.getDefaultInstance()) {
            r.executeTransaction(realm -> {
                realm.insertOrUpdate(exercise);
            });
        }

    }

    @Override
    public int getCount() {
        return 3;
    }
}

Edit: TL;DR: the problem is that the object is being seen by the realm.where()... after inserting an object prior to it only after reopening the app


Solution

  • You called api, it's will run on background so the code next to wrote on api call will execute. So your activity will close automatically.

    call your validation on your api success getting failed

    private void getExercisesSuccess(Exercise exercise) {
        try (Realm r = Realm.getDefaultInstance()) {
            r.beginTransaction();
            realm.insertOrUpdate(exercise);
            r.commitTransaction();
    
            //checking records are in realm
            final ArrayList<Exercise> exercise = new ArrayList<>();
            exercise.add(realm.where(Exercise.class).equalTo("name", exercise_name).findFirst());
            if (exercise.get(0) != null) {
                switch (pos) {
                    //returning new fragments depending on position
                }
            }
            //if exercise.get(0) doesn't exist at first, even tho I added it    afterwards it's still null here unless I reopen the app or let it crash
            finish();
            realm.close();
        }catch(Exception e){}
    }
    

    And remove below lines from your getItem()

      final ArrayList<Exercise> exercise = new ArrayList<>();
        exercise.add(realm.where(Exercise.class).equalTo("name", exercise_name).findFirst());
        if (exercise.get(0) != null) {
            switch (pos) {
                //returning new fragments depending on position
            }
        }
        //if exercise.get(0) doesn't exist at first, even tho I added it afterwards it's still null here unless I reopen the app or let it crash
        finish();
        realm.close();