首页 > 解决方案 > LiveData 查询在我第二次初始化时没有运行

问题描述

我有一份不同地雷的清单。每个矿井都有一个区块列表。

我有一个旋转器中的地雷和一个recyclerview中的块。

每当用户更改矿井微调器中的矿井时,我想显示不同的块列表

我在后端使用 Firebase 作为我的数据库。

当我在微调器中更改地雷时,我通过创建一个新的 MutableLiveData 来更新阻止列表,我在一个名为 FirebaseQueryLiveData 的类中扩展了它

我第一次使用包含矿名的查询初始化 FirebaseQueryLiveData 时,其中的所有事件都会触发。然而,在那之后,我打电话给它,没有任何事情发生。如果我在那里有断点,它会在构造函数中中断,但它永远不会到达 valueEventListener 中的 run() 方法、onActive() 方法或 onDataChanged。

我做了一些研究,并且看到了用 MutableLiveData 替换 LiveData 的建议。我已经这样做了,它似乎没有任何区别。

任何人都可以在代码中看到我可能遗漏的任何内容吗?我对 android 架构组件不是很熟悉,我从另一个有用的网站获得了 FirebaseQueryLiveData 类和教程,所以我正在努力了解我哪里出错了。

我做了一些研究,并且看到了用 MutableLiveData 替换 LiveData 的建议。我已经这样做了,它似乎没有任何区别。

    public class BlockListActivityViewModel extends ViewModel {

        private static DatabaseReference blockOutlineRef; // = FirebaseDatabase.getInstance().getReference(FireBasePaths.BLOCKOUTLINE.getPath("Therisa"));
        private static DatabaseReference mineListRef;

        private FirebaseQueryLiveData blockOutlineLiveDataQuery = null;
        private LiveData<BlockOutlineList> blockOutlineLiveData = null;

        private MediatorLiveData<String> selectedBlockNameMutableLiveData;

        private MediatorLiveData<ArrayList<String>> mineListMutableLiveData;

        public BlockListActivityViewModel() {

            User loggedInUser = UserSingleton.getInstance();

            setUpFirebasePersistance();
            setupMineLiveData(loggedInUser);
    //        setupBlockOutlineListLiveData();
        }

        private void setupBlockOutlineListLiveData(String mineName) {
            if (mineName != "") {
                blockOutlineRef = FirebaseDatabase.getInstance().getReference(FireBasePaths.BLOCKOUTLINE.getPath(mineName));

                blockOutlineLiveDataQuery = new FirebaseQueryLiveData(blockOutlineRef);
                blockOutlineLiveData = Transformations.map(blockOutlineLiveDataQuery, new BlockOutlineHashMapDeserialiser());
            }
        }

        private void setupMineLiveData(User user) {
            ArrayList<String> mineNames = new ArrayList<>();
            if (user != null) {
                if (user.getWriteMines() != null) {
                    for (String mineName : user.getWriteMines().values()) {
                        mineNames.add(mineName);
                    }
                }
            }

            setMineListMutableLiveData(mineNames);
            if (mineNames.size() > 0) {
                updateMineLiveData(mineNames.get(0));
            }
        }

        public void updateMineLiveData(String mineName) {
            SelectedMineSingleton.setMineName(mineName);
            setupBlockOutlineListLiveData(SelectedMineSingleton.getInstance());
        }

        public void setUpFirebasePersistance() {
            int i = 0;
    //        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        }

        private MutableLiveData<NamedBlockOutline> selectedBlockOutlineMutableLiveData;

        public MutableLiveData<NamedBlockOutline> getSelectedBlockOutlineMutableLiveData() {

            if (selectedBlockOutlineMutableLiveData == null) {
                selectedBlockOutlineMutableLiveData = new MutableLiveData<>();

            }
            return selectedBlockOutlineMutableLiveData;
        }

        public void setSelectedBlockOutlineMutableLiveData(NamedBlockOutline namedBlockOutline) {
            getSelectedBlockOutlineMutableLiveData().postValue(namedBlockOutline);
        }

        public MediatorLiveData<String> getSelectedBlockNameMutableLiveData() {
            if (selectedBlockNameMutableLiveData == null)
                selectedBlockNameMutableLiveData = new MediatorLiveData<>();

            return selectedBlockNameMutableLiveData;
        }

        public void setSelectedBlockNameMutableLiveData(String blockName) {
            selectedBlockNameMutableLiveData.postValue(blockName);
        }

        public MediatorLiveData<ArrayList<String>> getMineListMutableLiveData() {
            if (mineListMutableLiveData == null)
                mineListMutableLiveData = new MediatorLiveData<>();

            return mineListMutableLiveData;
        }

        public void setMineListMutableLiveData(ArrayList<String> mineListString) {
            getMineListMutableLiveData().postValue(mineListString);
        }
    private class BlockOutlineHashMapDeserialiser implements Function<DataSnapshot, BlockOutlineList>, android.arch.core.util.Function<DataSnapshot, BlockOutlineList> {
        @Override
        public BlockOutlineList apply(DataSnapshot dataSnapshot) {

            BlockOutlineList blockOutlineList = new BlockOutlineList();
            HashMap<String, NamedBlockOutline> blockOutlineStringHashMap = new HashMap<>();

            for (DataSnapshot childData : dataSnapshot.getChildren()) {

                NamedBlockOutline thisNamedOutline = new NamedBlockOutline();
                HashMap<String, Object> blockOutlinePointHeader = (HashMap<String, Object>) childData.getValue();
                HashMap<String, BlockPoint> blockOutlinePoints = (HashMap<String, BlockPoint>) blockOutlinePointHeader.get("blockOutlinePoints");

                thisNamedOutline.setBlockName(childData.getKey());
                thisNamedOutline.setBlockOutlinePoints(blockOutlinePoints);

                blockOutlineStringHashMap.put(childData.getKey(), thisNamedOutline);
            }

            blockOutlineList.setBlockOutlineHashMap(blockOutlineStringHashMap);
            return blockOutlineList;
        }
    }

    @NonNull
    public LiveData<BlockOutlineList> getBlockOutlineLiveData() {
        return blockOutlineLiveData;
    }

}

实时数据

public class FirebaseQueryLiveData extends MutableLiveData<DataSnapshot> {
    private static final String LOG_TAG = "FirebaseQueryLiveData";

    private final Query query;
    private final MyValueEventListener listener = new MyValueEventListener();

    private boolean listenerRemovePending = false;
    private final Handler handler = new Handler();

    public FirebaseQueryLiveData(Query query) {
        this.query = query;
    }

    public FirebaseQueryLiveData(DatabaseReference ref) {
        this.query = ref;

    }

    private final Runnable removeListener = new Runnable() {
        @Override
        public void run() {
            query.removeEventListener(listener);
            listenerRemovePending = false;
            Log.d(LOG_TAG, "run");
        }
    };

    @Override
    protected void onActive() {
        super.onActive();
        if (listenerRemovePending) {
            handler.removeCallbacks(removeListener);
            Log.d(LOG_TAG, "listenerRemovePending");

        }
        else {
            query.addValueEventListener(listener);
            Log.d(LOG_TAG, "addValueEventListener");

        }
        listenerRemovePending = false;
        Log.d(LOG_TAG, "listenerRemovePending");

    }

    @Override
    protected void onInactive() {
        super.onInactive();
        // Listener removal is schedule on a two second delay
        handler.postDelayed(removeListener, 4000);
        listenerRemovePending = true;
        Log.d(LOG_TAG, "listenerRemovePending");

    }

    private class MyValueEventListener implements ValueEventListener {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            setValue(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e(LOG_TAG, "Can't listen to query " + query, databaseError.toException());
        }
    }
}

标签: androidfirebasespinnerandroid-livedata

解决方案


推荐阅读