I've read about the differences, however I am still confused. I have an app where the user can swipe to decrease a number, and as soon as that number is decreased, a timer may start. This timer runs in the background, so that if the user comes back, it will resume at its corresponding time. As soon as the timer is finished, it will either reset, and wait for the user to swipe again, or it will remove itself from the view. When the user puts the app to the background, a node is created in Firebase. Whenever I have don't have any persistence enabled, the timer will work just fine. However, if I have persistence enabled, and the timer finishes while the app is in the background, the timer will not reset or remove itself, until I switch tabs in my app. To me, it seems like onDataChange is called while this node is being updated, thus dataSnapshot does not exist.
if (setsInt > 1) {
Log.d("set", "g");
home.myRef.child("workoutPause")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
home.recyclerView.post(new Runnable() {
@Override
public void run() {
Log.d("qn", "qk");
displayWorkoutAdapter.notifyItemChanged(displayWorkoutAdapter.rowTypeList.size() - 2);
context.unregisterReceiver(secondReceiver);
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
((RestRowModel) list.get(list.size() - 2)).setMinutes(String.format("%s", inputtedMin));
((RestRowModel) list.get(list.size() - 2)).setSeconds(String.format("%s", inputtedSec));
SaveChanges saveChanges = new SaveChanges(home, displayWorkoutAdapter);
saveChanges.saveToDB("workoutPause", list, true, null);
}
This is called once the timer is finished while the user has the app in the foreground, or when the timer finishes while the app is in the background, and the user returns to the app. I also need the whole app to be able to run in offline for a short period of time, in case network interruptions occur. Am I right to assume, that I should use setPersistenceEnabled()?
UPDATE saveToDB method
DatabaseReference databaseReference;
List<RowType> list = null;
if(rowTypeList != null) {
list = new ArrayList<>(rowTypeList);
}
if(list != null && rowTypeList.size() > 0) {
list.remove(0);
}
if (time != null) {
WorkoutTime workoutTime = new WorkoutTime();
workoutTime.setTime(time);
String timeTable = tableName + "TimeTable";
home.myRef.child("workouts").child(timeTable).setValue(workoutTime);
}
else if(time == null && list != null){
for (int i = 0; i < list.size(); i++) {
RowType rowType = list.get(i);
if (rowType.getClass() == ExerciseModel.class) {
ExerciseModel exerciseModel = (ExerciseModel) rowType;
exerciseModel.setView(exerciseView);
} else if (rowType.getClass() == RestRowModel.class) {
RestRowModel restRowModel = (RestRowModel) rowType;
restRowModel.setView(restView);
} else if (rowType.getClass() == SupersetRowModel.class) {
SupersetRowModel supersetRowModel = (SupersetRowModel) rowType;
supersetRowModel.setViewType(supersetView);
} else if (rowType.getClass() == PyramidsetRowModel.class) {
PyramidsetRowModel pyramidsetRowModel = (PyramidsetRowModel) rowType;
pyramidsetRowModel.setViewType(pyramidSetView);
}
}
if(pause){
databaseReference = home.myRef.child(tableName);
}
else{
databaseReference = home.myRef.child("workouts").child(tableName);
}
DatabaseReference newRef = databaseReference.child("list");
newRef.setValue(list);
}