1

I'm trying to make a search query using firebase and it works really well.

Query searchQuery = usersRef.orderByChild(child).startAt(text).endAt(text + "\uf8ff").limitToFirst(limit);
    searchQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //search result logic
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });

I have enabled offline capabilities because in need them in my app and here is my problem: this ValueEventListener keep listening on the local stored datas while I need to keep it always updated... it's a search query.

I don't think keepSynced(true) applied to the users reference would be a nice idea because this reference is huge and this will cause lot of internet and memory waste.

I've red on web that using addValueEventListener instead of addListenerForSingleValueEvent query will always receive updated answers from the server, without look in local stored data. This could be a way to solve my problem but I need to listen data ONCE and this will keep listen for datas so should I stop this listener if I decide to use it?

Please help me with some ideas and sorry for my scholastic English.

Giorgio
  • 65
  • 10
  • Using disk persistence and `addListenerForSingleValueEvent` don't mix. If you want to use disk persistence, you should use `addValueListener` to get both the cached value (straight away) and possilbly the updates server value (later) (which in general is recommended anyway). See my longer explanation here: https://stackoverflow.com/questions/34486417/firebase-offline-capabilities-and-addlistenerforsinglevalueevent – Frank van Puffelen Mar 03 '18 at 15:41
  • If this is a real problem for you, consider using [Cloud Firestore](https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document). The `get()` method there will always check the server-side value while the client is connect, and only return the cached value when the client is offline. – Frank van Puffelen Mar 03 '18 at 15:44

2 Answers2

1

Well you could hold a reference to your event listener and remove it from the reference when you dont need it.

myEventListener = ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        //happy place
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
       //error
    }
});

Somewhere else:

ref.removeEventListener(myEventListener);
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
1

If you have enabled offline capabilities, it means that you'll be able to use your app even if it temporarily loses its network connection. So the ValueEventListener will keep listening on the local stored datas as long as you are offline. If you want the results to be updated, then you need to go online.

If you want to listen to data only once, then you need to use: addListenerForSingleValueEvent(). If you want to use addValueEventListener, just don't forget to remove the listener according to the life-cycle of your activity as I have already answered here.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193