0

I am trying to get the data from firestore using where query but on calling the get() and OnComplete() methods, my program's control won't go inside the oncomplete method.

When I return the HashMap it always returns null and even tried checking through log if control goes inside the OnComplete method but it doesn't.

Edit: The onFailure() method doesn't work either.

HashMap<String,String> doesMeetingPointExists(LatLng latLng)
{

        String lat = String.valueOf(latLng.getLatitude());

        String longi = String.valueOf(latLng.getLongitude());

        final HashMap<String,String> jsonObject = new HashMap<>();

        Query q = firebaseFirestore.collection("meeting_points").whereEqualTo("latitude",lat).whereEqualTo("longitude",longi);
        q.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {

                if (task.getResult().isEmpty()){
                    jsonObject.put("isNull", "true");
                    Log.d("TAG","DATA"+jsonObject.toString());
                }
                else{
                    Log.d("TAG", "IN ON Fail");
                    QuerySnapshot querySnapshot = task.getResult();
                    for (QueryDocumentSnapshot documentSnapshot: querySnapshot){
                        Map<String,Object> map = documentSnapshot.getData();
                        jsonObject.put("isNull","false");
                        jsonObject.put("LocationName", String.valueOf(map.get("place_name")));
                        jsonObject.put("LocationAddress", String.valueOf(map.get("place_address")));
                        Log.d("TAG","DATA"+jsonObject.toString());

                    }
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d("TAG", "IN ON Fail");
                jsonObject.put("isNull", "true");
                Timber.e("Errora"+"---"+e.getMessage());
            }
        });
        Log.d("TAG",jsonObject.toString());
        return jsonObject;
    }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Tirth Mehta
  • 329
  • 2
  • 9
  • Even if you believe the callbacks are not being invoked, you should know that `get()` is asynchronous and returns immediately *before* the callbacks are ever invoked. That means your function is always going to return a HashMap that is initially empty. – Doug Stevenson Nov 09 '19 at 12:02
  • Can you suggest a way to return the hashmap in a way such that it is not empty and I can access the data in some other class? – Tirth Mehta Nov 09 '19 at 12:13
  • You won't be able to return it synchronously without also risking your app crashing with an ANR if the response is slow. You could have the caller pass in their own callback, and invoke it when the data is ready. Or use Kotlin coroutines. – Doug Stevenson Nov 09 '19 at 12:25
  • For an example of a custom callback, see: https://stackoverflow.com/questions/51000169/how-to-check-a-certain-data-already-exists-in-firestore-or-not/51002413#51002413 – Frank van Puffelen Nov 09 '19 at 14:42

0 Answers0