Summary of problem: I am new to Android Studio and Firebase with Geofire, please be gentle. My goal is to log the user out when they are on the map screen of my app without a Geofire location. I cannot figure out how to query "g" in my Geofire. How do I query the "g" child to see if it was created or not? And if "g" was not created, they need to be logged out. So the goal is query for "g" and create method that if "g" is null, logs user out.
Things I have done: Many Stackoverflow posts were not using Android Studio. I did find this I have inserted location using geofire in firebase but it did not resolve my issue. I also tried Google firebase check if child exists
onLocationChanges is where I add the Online Vendor and where I want to also check to see if "g" was created or not.
@Override
public void onLocationChanged(Location location) {
// Adds VendorOnline Child to Firebase when Vendor is On This Activity
addVendorOnline();
// Log user out if they are on the map screen without a "VendorOnline" Uid
logVendorOutIfBuggy();
}
This is the method to create the Online Vendor and check if "g" was created:
private void addVendorOnline(){
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
String vendorId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference vendorIdReference = FirebaseDatabase.getInstance().getReference("VendorOnline");
GeoFire vendorGeoFire = new GeoFire(vendorIdReference);
vendorGeoFire.setLocation(vendorId, new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()));
}
}
// Log user out if for some reason they do not have a "g" child but are on the map screen.
private void logVendorOutIfBuggy() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("g");
if(ref == null){
Toast.makeText(VendorMapsActivity.this, "Error", Toast.LENGTH_LONG).show();
FirebaseAuth.getInstance().signOut();
}
What I expect to happen and what actually happens: I expect the user to be logged out if "g" is equals null, but right now the user does not get logged out.
Here's a sample of calling getLocation
to see if vendor registered a location.
From javadocs of LocationCallback.onLocationResult
:
This method is called with the current location of the key. location will be null if there is no location stored in GeoFire for the key.
final String vendorKey = "someVendorKey";
geoFire.getLocation(vendorKey, new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
if (key.compareTo(vendorKey) == 0) {
if (location == null) {
// vendor has not logged a location yet (or it was removed)
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});