没有得到确切的结果,它应该给我在Firebase查询中

时间:2018-08-19 19:33:48

标签: android firebase firebase-realtime-database

这样在文本观察器中被查询,但是当任何用户更改数据时,都不会发现新的更改数据

TextWatcher tw = new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        getSupportFragmentManager().beginTransaction().remove(searchMainFragment);
        quickResultsList.clear();
        quickResultsAdapter.notifyDataSetChanged();
        if(!s.toString().isEmpty()){
            Log.e(TAG, "onDataChange: "+ s.toString());
            Query query =  application.getMRef().child(getString(R.string.db_user_details)).orderByChild(getString(R.string.db_displayName))
                    .startAt(s.toString().toLowerCase().trim()).endAt(s.toString().toLowerCase().trim() +"\uf8ff");
                /* query.keepSynced(true); */
                query.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            for(DataSnapshot single : dataSnapshot.getChildren()){
                                quickResultsAdapter.add(single.child(getString(R.string.db_displayName)).getValue().toString());
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });

        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        //application.getMRef().child(getString(R.string.db_user_details)).keepSynced(false);
        Log.e(TAG, "afterTextChanged: "+ " text has been changed");
    }
} ;

如果我使用keepsynced,那么它将运行,但是如果我不使用它,则无法正常工作。我不想将这么多数据同步到缓存中?

3 个答案:

答案 0 :(得分:1)

使用addListenerForSingleValueEvent时,侦听器将精确地读取一次数据。这意味着您的onDataChange被调用为当前值(如果有,则从缓存中调用,否则从服务器中调用),然后立即停止监听。

如果要继续监听数据更改,则应使用addValueEventListener。使用此方法意味着(与addListenerForSingleValueEvent一样),您的onDataChange将立即被当前数据调用,但是(与addListenerForSingleValueEvent不同),监听器将在此之后保持活动状态,而您的onDataChange也会要求以后进行更改。

答案 1 :(得分:0)

最好在块外使用它

Query query =  application.getMRef().child(getString(R.string.db_user_details)).orderByChild(getString(R.string.db_displayName))
                .startAt(s.toString().toLowerCase().trim()).endAt(s.toString().toLowerCase().trim() +"\uf8ff");
            /* query.keepSynced(true); */
            query.addChildEventListener (new ChildEventListener() {

                    @Override
                    public void onChildAdded (@NonNull DataSnapshot dataSnapshot) {
                        for(DataSnapshot single : dataSnapshot.getChildren()){
                            quickResultsAdapter.add(single.child(getString(R.string.db_displayName)).getValue().toString());
                        }
                    }
                    @Override
                    public void onChildChanged(@NonNull DataSnapshot dataSnapshot) {
                        for(DataSnapshot single : dataSnapshot.getChildren()){
                            quickResultsAdapter.add(single.child(getString(R.string.db_displayName)).getValue().toString());
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

但这也可以

TextWatcher tw = new TextWatcher() {
boolean isMethodCalled = false;
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        getSupportFragmentManager().beginTransaction().remove(searchMainFragment);
        quickResultsList.clear();
        quickResultsAdapter.notifyDataSetChanged();
        if(!s.toString().isEmpty() && !isMethodCalled ){
            Log.e(TAG, "onDataChange: "+ s.toString());
               Query query =  application.getMRef().child(getString(R.string.db_user_details)).orderByChild(getString(R.string.db_displayName))
                .startAt(s.toString().toLowerCase().trim()).endAt(s.toString().toLowerCase().trim() +"\uf8ff");
            /* query.keepSynced(true); */
            query.addChildEventListener (new ChildEventListener() {

                    @Override
                    public void onChildAdded (@NonNull DataSnapshot dataSnapshot) {
                        for(DataSnapshot single : dataSnapshot.getChildren()){
                            quickResultsAdapter.add(single.child(getString(R.string.db_displayName)).getValue().toString());
                        }
                    }
                    @Override
                    public void onChildChanged(@NonNull DataSnapshot dataSnapshot) {
                        for(DataSnapshot single : dataSnapshot.getChildren()){
                            quickResultsAdapter.add(single.child(getString(R.string.db_displayName)).getValue().toString());
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
isMethodCalled  = true;

        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        //application.getMRef().child(getString(R.string.db_user_details)).keepSynced(false);
        Log.e(TAG, "afterTextChanged: "+ " text has been changed");
    }
} ;

答案 2 :(得分:0)

TextWatcher tw = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        getSupportFragmentManager().beginTransaction().remove(searchMainFragment);
        quickResultsList.clear();
        quickResultsAdapter.notifyDataSetChanged();
        if(!s.toString().isEmpty()){
            Log.e(TAG, "onDataChange: "+ s.toString());
            Query query =  application.getMRef().child(getString(R.string.db_user_details)).orderByChild(getString(R.string.db_displayName))
                    .startAt(s.toString().toLowerCase().trim()).endAt(s.toString().toLowerCase().trim() +"\uf8ff");
            query.addChildEventListener (new ChildEventListener() {

                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    for(DataSnapshot single : dataSnapshot.getChildren()){
                        if(single.getKey().equals("displayName")){
                            Log.e(TAG, "onChildAdded: "+ single.getValue().toString());
                        }
                    }
                    //Log.e(TAG, "onChildAdded: "+ dataSnapshot);
                }

                @Override
                public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    for(DataSnapshot single : dataSnapshot.getChildren()){
                        try {
                            if(single.getKey().equals("displayName")){
                                Log.e(TAG, "onChildChanged: "+ single.getValue().toString());
                            }
                        }catch (NullPointerException e){
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        //application.getMRef().child(getString(R.string.db_user_details)).keepSynced(false);
        Log.e(TAG, "afterTextChanged: "+ " text has been changed");
    }
} ;
通过使用

我得到了我的结果,它也是活动的侦听器,所以我不必担心同步过程。