onDataChanged на Android возвращает значения дважды для изменения базы данных - PullRequest
0 голосов
/ 30 мая 2019

Каждый раз, когда значение в базе данных обновляется, я наблюдаю за изменениями в моей Деятельности. По какой-то причине изменения запускаются дважды.Один раз со старым значением и один раз с новым.Однако они не сохраняют этот порядок.То есть иногда я сначала получаю старое значение, а иногда сначала новое.

Я посмотрел проблему переполнения стека и натолкнулся на проблему с включенным постоянством.Я также пытался установить для параметра «Persistence Enabled» значение false, но все равно не повезло :(.

// a view model for a data entity called topics
// the topicsHashMap is a Mutable Live data and being observed actively in the activity
//the hashmap is structured as follows: a community has multiple topics so hashmap has community id as key and arraylist of topics as items under the key in the hashmap
mTopicsViewModel.getTopicsHashMap().observe(mActivity, new Observer<HashMap<String, ArrayList<Topic>>>() {
    @Override
    public void onChanged(HashMap<String, ArrayList<Topic>> topicsHashMap) {
        //getting the topics here
        ArrayList<Topic> topics = topicsHashMap.get(mCommunityID);
        //getting the current topics from the array list
        Topic newTopic = topicsHashMap.get(mCommunityID).get(topics.indexOf(new Topic(mTopicID,null,null,null,0)));
        //voting is an attribute of the topic object, I need to observe the voting changes in my activity. These changes are being returned twice.
        //checking if current voting is not same as the new voting then change all ui
        if(mCurrentVoting!=newTopic.voting){
            mCurrentTopic = newTopic;
            mCurrentVoting = mCurrentTopic.voting;
            //voting alert dialog reflects all these changes in voting in real time
            if(votingAlertDialog!=null){
                if(votingAlertDialog.isShowing()) {
                    //then you need to update the views inside voting alert dialog as well
                    if (!userOption.name.isEmpty()) {
                        Logger.d("New voting added "+mCurrentVoting);
                        // calling the function that changes the ui accordingly
                        showVotingChangesOnAlertDialog();
                    }
                }
            }
        }else{
            Logger.d("Ignoring this voting change because its similar to previous");
        }


    }
});

Ожидается только один вызов onDataChanged для каждого изменения базы данных; Однако я получаю два.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...