Этот экземпляр Realm уже закрыт - PullRequest
0 голосов
/ 02 ноября 2018

Я занимаюсь разработкой cv-приложения и использую область для сохранения ответов о модернизации, но получаю следующее исключение:

FATAL EXCEPTION: main
Process: activity.drawer.navigation.com.kitabsawticlone, PID: 5400
java.lang.IllegalStateException: This Realm instance has already been closed, making it unusable.
    at io.realm.BaseRealm.checkIfValid(BaseRealm.java:435)
    at io.realm.Realm.where(Realm.java:1368)
    at activity.drawer.navigation.com.kitabsawticlone.IntroductionItem$1.onResponse(IntroductionItem.java:60)

Мой вводный класс.java:

public class IntroductionItem extends AppCompatActivity {

    public RealmList<Introduction> introductionList;

    public IntroductionAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.introduction);
        KitabInterface kitabInterface = ApiClient.getApiService();
        Call<KitabSawti> call = kitabInterface.getIntroduction();

        call.enqueue(new Callback<KitabSawti>() {
            @Override
            public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {
                introductionList = response.body().getIntroduction();
                Realm.init(IntroductionItem.this);

                RealmConfiguration config = new RealmConfiguration.Builder()
                        .name("overview.realm")
                        .schemaVersion(1)
                        .deleteRealmIfMigrationNeeded()
                        .build();
                Realm.setDefaultConfiguration(config);


                // add response to realm database
                Realm realm = Realm.getInstance(config);
                realm.beginTransaction();
                realm.copyToRealmOrUpdate(introductionList);
                realm.commitTransaction();
                realm.close();


// programmatically check : data is inserted in to realm or not

                int notesCount = realm.where(KitabSawti.class).findAll().size();
                int notesCount2 = realm.where(Introduction.class).findAll().size();


                Log.d("my first", String.valueOf(notesCount));
                Log.d("my second", String.valueOf(notesCount2));


                RecyclerView recyclerView = findViewById(R.id.recyclerView);
                recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                adapter = new IntroductionAdapter(IntroductionItem.this, introductionList); // changes
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<KitabSawti> call, Throwable t) {

            }
        });
    }
}

1 Ответ

0 голосов
/ 02 ноября 2018

Когда вы используете realm.close();, вы закрываете область, переместите эту строку в конец своего вызова. Как это:

public class IntroductionItem extends AppCompatActivity {

    public RealmList<Introduction> introductionList;

    public IntroductionAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.introduction);
        KitabInterface kitabInterface = ApiClient.getApiService();
        Call<KitabSawti> call = kitabInterface.getIntroduction();

        call.enqueue(new Callback<KitabSawti>() {
            @Override
            public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {
                introductionList = response.body().getIntroduction();
                Realm.init(IntroductionItem.this);

                RealmConfiguration config = new RealmConfiguration.Builder()
                        .name("overview.realm")
                        .schemaVersion(1)
                        .deleteRealmIfMigrationNeeded()
                        .build();
                Realm.setDefaultConfiguration(config);


                // add response to realm database
                Realm realm = Realm.getInstance(config);
                realm.beginTransaction();
                realm.copyToRealmOrUpdate(introductionList);
                realm.commitTransaction();



// programmatically check : data is inserted in to realm or not

                int notesCount = realm.where(KitabSawti.class).findAll().size();
                int notesCount2 = realm.where(Introduction.class).findAll().size();


                Log.d("my first", String.valueOf(notesCount));
                Log.d("my second", String.valueOf(notesCount2));


                RecyclerView recyclerView = findViewById(R.id.recyclerView);
                recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                adapter = new IntroductionAdapter(IntroductionItem.this, introductionList); // changes
                recyclerView.setAdapter(adapter);
                realm.close();
            }

            @Override
            public void onFailure(Call<KitabSawti> call, Throwable t) {

            }
        });
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...