我有一个活动,我在其中存储来自Api的信息并将其存储在数据库中 在小组中,我从数据库中读取了此信息 但是问题在于,当我第一次运行该程序时,不会读取信息,但是当我关闭程序并重新运行时,则可以正确读取信息。
如何解决此问题?
主要活动中保存到领域的代码
//save to realm
realm.beginTransaction();
for (int i = 0; i < currentWeatherModel.size(); i++)
realm.copyToRealm(currentWeatherModel.get(i));
realm.commitTransaction();`
片段中从领域读取的代码
private void readDatabase() {
RealmResults<CurrentWeatherModel> results = realm.where(CurrentWeatherModel.class).findAll();
for (int i = 0; i < results.size(); i++) {
String icon = results.get(i).getIcon();
double temp = results.get(i).getTemp();
int humidity = results.get(i).getHumidity();
double speed = results.get(i).getSpeed();
int all = results.get(i).getAll();
int id = results.get(i).getId();
String description = results.get(i).getDescription();
//Toast.makeText(getActivity().getApplicationContext(), temp+"a", Toast.LENGTH_SHORT).show();
}