Firebase setPersistenceEnabled vs keepSynced

时间:2017-12-30 03:18:20

标签: android firebase firebase-realtime-database timer

我已经读过差异,但我仍然感到困惑。我有一个应用程序,用户可以滑动以减少一个数字,一旦该数字减少,计时器可能会启动。此计时器在后台运行,因此如果用户返回,它将在相应的时间恢复。一旦计时器完成,它将重置,并等待用户再次滑动,否则它将从视图中移除。当用户将应用程序置于后台时,将在Firebase中创建一个节点。每当我没有启用任何持久性时,计时器就可以正常工作。但是,如果我启用了持久性,并且计时器在应用程序处于后台时结束,则计时器将不会重置或自行删除,直到我在应用程序中切换选项卡。对我来说,似乎在更新此节点时调用了onDataChange,因此dataSnapshot不存在。

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);

                    }

当用户将应用程序置于前台时,或者当应用程序在后台时计时器结束,并且用户返回应用程序时,一旦计时器完成,就会调用此方法。我还需要整个应用程序能够在短时间内离线运行,以防网络中断。我可以假设,我应该使用setPersistenceEnabled()?

UPDATE saveToDB方法

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);
    }

0 个答案:

没有答案