该Realm实例已关闭

时间:2018-11-02 17:29:07

标签: android realm

我正在开发一个简历应用程序,并使用领域来保存改造响应,但出现以下异常:

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)

我的IntroductionItem.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 :(得分:1)

使用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) {

            }
        });
    }
}