android - 只有在重新打开应用程序后,Realm 才创建插入的对象

标签 android realm

基本上我试图在尝试使用对象之前检查它是否存在,如果不存在,我会预先添加它然后尝试使用它。

如果它不存在,它会被正确添加,但之后应用程序仍然找不到它,除非我重新打开应用程序

一切都按预期工作,但是,如果对象在那里开始

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;
    }
}

编辑:TL;DR:问题是对象在 realm.where()... 之前插入一个对象后只有在重新打开应用程序后才能被 realm.where()... 看到

最佳答案

你调用了 api,它会在后台运行,所以写在 api 调用旁边的代码会执行。因此您的 Activity 将自动关闭。

在你的 api 成功时调用你的验证失败

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){}
}

并从您的 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();

关于android - 只有在重新打开应用程序后,Realm 才创建插入的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50759723/

相关文章:

php - 在 AsyncTask 上的 android 错误中更改密码功能

java - 如何在关闭android中的另一个 Activity 后关闭主要 Activity ?

ios - Realm - 对象映射和持久化 AnyObject 类型

android - Realm.IO - 可以使用 createOrUpdateAllFromJson 解析 JSON 数组吗?

swift - Realm 中的可选整数

带有 Digimarc 的 Android Unity3d 和 VUForia。初始化失败!您的硬件不支持此应用程序

java - 无法滚动下面有 GridView 和 ListView 的屏幕

swift - 为什么 Realm 数据库属性值不改变?

ios - ld : symbol(s) not found for architecture x86_64 (realm)

java - 将用Java编写的.jar导入Android,noClassDefFoundError