领域数据库从Android读取数据 [英] Realm Database read data from Android

查看:42
本文介绍了领域数据库从Android读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是realm数据库的新手,因此需要一些帮助.当我杀死我的应用程序,然后再次回到该应用程序时,我无法检索保存在retrofit中的数据.插入效果很好.这是我的代码:

I am new to realm database so need some help. When I kill my app then come back to the app again, I am unable to retrieve the data which was saved in retrofit. Insertion is working perfectly. Here is my code :

在callJson方法内插入代码:

Insert Code inside callJson Method:

 realm.beginTransaction();
 realmCities = realm.copyToRealm(clientContactList);
 realm.commitTransaction();

在我登录时插入后的响应:

Response after inserting when i am logging :

path :  /data/data/com.xyz.da/files/default.realm
response from realm : E/Output: ClientContact = proxy[{clientName:A},{email:mit@email.com},{address:Palava},{contactPerson:Amit},{numberMobile:9930182464},{numberLandline:25123456},{groupName:B},{tumbnailColor:0}]ClientContact = proxy[{clientName:A1},{email:abc@email.com},{address:MBP},{contactPerson:John},{numberMobile:9876543210},{numberLandline:25123456},{groupName:B1},{tumbnailColor:0}]ClientContact = proxy[{clientName:A10},{email:abc@email.com},{address:MBP},{contactPerson:John},{numberMobile:9876543210},{numberLandline:25123456},{groupName:E2},{tumbnailColor:0}]

在onCreateView中,我这样做是这样的:在这里,我杀死应用程序并返回realmCities列表为空,使其再次命中callJson()

In onCreateView I am doing like this : Here when i kill app and come back realmCities List coming null so it again hitting callJson()

if(realmCities!=null && realmCities.size()>0){
    Log.e("realmCities",""+realmCities.size());
}else{
    callJSON();
}

推荐答案

您已插入数据,但是当应用重新启动时,您必须从数据库中获取数据

you have inserted data but when app is restart you have to get data from database

 realm.beginTransaction();
 realmCities = realm.copyToRealm(clientContactList);
 realm.commitTransaction();

重新启动应用程序时,您必须从数据库中获取数据,如:

when restart the app you have to get data from database like :

RealmResults<ClientContact> realmCities= realm.where(ClientContact.class).findAllAsync();    
                        //fetching the data
        realmCities.load();

然后您必须检查状况

if(realmCities!=null && realmCities.size()>0){
    Log.e("realmCities",""+realmCities.size());
}else{
    callJSON();
}

这篇关于领域数据库从Android读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆