Store data into Realm through Retrofit 2

☆樱花仙子☆ 提交于 2019-12-06 21:04:31

I got my answer from Realm official website and other blogs.

My above code is working perfect. Just need to add Realm functionality properly.

In Activity : Dashboard

call.enqueue(new Callback<RealmList<ExampleTest>>() {
            @Override
            public void onResponse(Call<RealmList<ExampleTest>> call, Response<RealmList<ExampleTest>> response) {

                resource = response.body();


              Realm.init(DashboardActivity.this);
              config = new RealmConfiguration.Builder()
                        .name("books.realm")
                        .schemaVersion(1)
                        .deleteRealmIfMigrationNeeded()
                        .build();
                Realm.setDefaultConfiguration(config);


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


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

                int notesCount = realm.where(ExampleTest.class).findAll().size();
                int notesCount2 = realm.where(ListConditionalQuestion.class).findAll().size();
                int notesCount3 = realm.where(LstHotSpot.class).findAll().size();

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


            }


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

   Log.d("fail","response fail");

            }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!