private void loadData() {
   // Check if GPS is enabled and fetch posts
   if (Utils.isGPSEnabled(this)) {
     if (lastLocation == null || isLoctionStale(lastLocation)) {
       connectToGoogleAPI();
     } else {
       getPosts();
     }
   } else {
     refreshLayout.setRefreshing(false);
     Toast.makeText(getApplicationContext(), "GPS is disabled", Toast.LENGTH_SHORT).show();
   }
 }
  private void getPosts() {
    // Check if internet is enabled first
    if (!Utils.isInternetEnabled(getApplicationContext())) {
      Toast.makeText(getApplicationContext(), "No internet connection", Toast.LENGTH_SHORT).show();
      refreshLayout.setRefreshing(false);
    } else {
      // Retreive posts by first fetching post locations near current locations
      // and then fetching the post details for those posts
      locationKeys = new ArrayList<>();
      posts = new ArrayList<>();
      // Get post locations in range
      final GeoQuery query =
          geofireRef.queryAtLocation(
              new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()),
              postRangeInKm);
      query.addGeoQueryEventListener(
          new GeoQueryEventListener() {
            @Override
            public void onKeyEntered(String key, GeoLocation location) {
              // Once a post location is received add it to the list
              locationKeys.add(key);
            }

            @Override
            public void onKeyExited(String key) {}

            @Override
            public void onKeyMoved(String key, GeoLocation location) {}

            @Override
            public void onGeoQueryReady() {
              // Once all posts locations are retrieved, remove listener
              query.removeGeoQueryEventListener(this);
              // If no posts stop refreshing
              if (locationKeys.size() == 0) {
                refreshLayout.setRefreshing(false);
              }
              // TODO: Should this br done off the UI thread? Where
              // would the listeners run?
              for (final String key : locationKeys) {
                // For each location post, retreive its post details
                firebaseRef
                    .child(key)
                    .addListenerForSingleValueEvent(
                        new ValueEventListener() {
                          @Override
                          public void onDataChange(DataSnapshot dataSnapshot) {
                            // Create a post object out of the details
                            QueryPosts post = dataSnapshot.getValue(QueryPosts.class);
                            // If post is stale, delete it. This should be done server side,
                            // but we don't have one
                            if (isPostStale(post)) {
                              firebaseRef.child(key).removeValue();
                              geofireRef.removeLocation(key);
                            } else {
                              // Otherwise, add it to the list of post details
                              posts.add(new Post(key, post.getMessage(), post.getTimestamp()));
                            }
                            // Remove post location, if details have been fetched and
                            // once all posts are fetched, call onSucces
                            locationKeys.remove(key);
                            if (locationKeys.size() == 0) {
                              onSuccess();
                            }
                          }

                          @Override
                          public void onCancelled(FirebaseError firebaseError) {
                            onError(firebaseError);
                          }
                        });
              }
            }

            @Override
            public void onGeoQueryError(FirebaseError error) {
              onError(error);
            }
          });
    }
  }