如何根据 LiveData.observe 收到的信息更新 ROOM 数据库?
How to update ROOM Database on information received on LiveData.observe?
I have this problem in two key places throughout my code, and can't solve it. I have the following problem: I want to retrieve information from by database and update it all at the same time. I have a room database with an ordinary repository and viewmodel. And my code is this:
daysViewModel.getStopsBiggerThan(day).observe(this, new Observer<List<DayActivity>>() {
@Override
public void onChanged(List<DayActivity> dayActivities) {
Log.d("stop_please_in_bigger", dayActivities.toString());
if (sort == 1) {
for (DayActivity stop : dayActivities) {
DayActivity new_stop = new DayActivity(stop.getTitle_Day(), stop.getLocation_Day(), stop.getStars_Day(),
stop.getOpening_hours_1_Day(), stop.getOpening_hours_2_Day(), stop.getOpening_hours_3_Day(),
stop.getNotas_Day(), stop.getDay_Day() - 1, stop.getStarttime_Day(),
stop.getEndtime_Day(), stop.getOrder_Day());
new_stop.setId_Day(stop.getId_Day());
daysViewModel.update(new_stop);
}
}
}
});
Firstly, I have tried to use a simple list instead of a LiveData but then it just returned a null object. I have also tried to include a variable to act like a switch "sort" and it didin't work.
As most of you probably have understood by now, the problem is that by changing the database query information on an onChanged void, I am feeding a vicious loop.
Turns out this was a stupid question... Just try and define a variable and play around with it!
I have this problem in two key places throughout my code, and can't solve it. I have the following problem: I want to retrieve information from by database and update it all at the same time. I have a room database with an ordinary repository and viewmodel. And my code is this:
daysViewModel.getStopsBiggerThan(day).observe(this, new Observer<List<DayActivity>>() {
@Override
public void onChanged(List<DayActivity> dayActivities) {
Log.d("stop_please_in_bigger", dayActivities.toString());
if (sort == 1) {
for (DayActivity stop : dayActivities) {
DayActivity new_stop = new DayActivity(stop.getTitle_Day(), stop.getLocation_Day(), stop.getStars_Day(),
stop.getOpening_hours_1_Day(), stop.getOpening_hours_2_Day(), stop.getOpening_hours_3_Day(),
stop.getNotas_Day(), stop.getDay_Day() - 1, stop.getStarttime_Day(),
stop.getEndtime_Day(), stop.getOrder_Day());
new_stop.setId_Day(stop.getId_Day());
daysViewModel.update(new_stop);
}
}
}
});
Firstly, I have tried to use a simple list instead of a LiveData but then it just returned a null object. I have also tried to include a variable to act like a switch "sort" and it didin't work.
As most of you probably have understood by now, the problem is that by changing the database query information on an onChanged void, I am feeding a vicious loop.
Turns out this was a stupid question... Just try and define a variable and play around with it!