1

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);
    }
Gerald Tan
  • 91
  • 14
  • As I explained [here](https://stackoverflow.com/questions/34486417/firebase-offline-capabilities-and-addlistenerforsinglevalueevent) combining `addListenerForSingleValueEvent()` with `setPersistenceEnabled()` is a recipe for confusion. If you must use disk persistence, use regular `addValueListener` calls (which in general is recommended). – Frank van Puffelen Dec 30 '17 at 03:36
  • Thanks for the reference. It definitely makes a lot more sense now. How would I view the local copy of the data? Changing the type of listener still produced the same results. I have updated it with more code so that it may be more clear to you what I am doing. It doesn't make sense to me why if persistence is not enabled, dataSnapshot shows as existing, and if it is enabled, it does not show as existing. – Gerald Tan Dec 30 '17 at 06:06
  • 1
    I fixed it by replacing the listener with a completion listener to setvalue in saveToDB. – Gerald Tan Dec 30 '17 at 06:36

0 Answers0