firebase / geofire-objc

GeoFire for Objective-C - Realtime location queries with Firebase
MIT License
436 stars 180 forks source link

Remove Key Query Not Deleting in DB #111

Closed ccynn closed 5 years ago

ccynn commented 6 years ago

Firebase Version: 4.10.1 GeoFire Version: 2.0.1

As the title suggests, I am not seeing a key being removed from the realtime database when running the remove geofire query although the code executes successfully without error. No issues in reading, adding or updating a key.

Any guidance is appreciated, I have the relevant code below:

    let confirmDelete = UIAlertController(title: "Confirm Delete", message: "You sure?", preferredStyle: .alert)
    let yesAction = UIAlertAction(title: "Yes", style: .default) { (alertAction) in

        self.geoFire.removeKey("\(key)", withCompletionBlock: { (error) in
            guard error == nil else { print(error)
                return
            }
            print("\(key) location deleted!")
        })
    }
    let noAction = UIAlertAction(title: "No", style: .cancel, handler: nil)
    confirmDelete.addAction(yesAction)
    confirmDelete.addAction(noAction)
    self.present(confirmDelete, animated: true, completion: nil)
ghost commented 6 years ago

I can confirm this as I just came across it while testing. My guess it that the culprit lies in GeoFire.m line 97-98

[[self firebaseRefForLocationKey:key] updateChildValues:value withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {

value is set to nil when deleting a location, but this just leads to the update being ignored without triggering an error. IMHO it would be better to use removeValue instead when deleting.

ghost commented 6 years ago

@ccynn As a workaround until this is fixed, you can safely delete the key yourself directly. Here is my code for this:

            let collectionRef = Database.database().reference()
                .child("groupId")
                .child("locationId")

            collectionRef.removeValue { error, ref in

                if error != nil {
                    reject(error!)
                    return
                }

                fulfill(poi)

            }

I am using a callback and I am wrapping the remove in a promise. If you just want to remove and don't care if it worked or not, just call

collectionRef.removeValue()

wf9a5m75 commented 6 years ago

I am also surprised this bug still alive in the latest version.

For object-c developer,


  [[[geoFire.firebaseRef child:key] child:@"l"] removeValueWithCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {

    if (error == nil) {
      [[[geoFire.firebaseRef child:key] child:@"g"] removeValue];
    }
  }];
YasirAmeen commented 6 years ago

I am also facing this issue, i did the workaround mention by ghowen, but this causing crash on Android side, here is the log.

Fatal Exception: java.lang.AssertionError: Got Datasnapshot without location with key 45 at com.firebase.geofire.GeoQuery.childChanged(GeoQuery.java:294) at com.firebase.geofire.GeoQuery.access$100(GeoQuery.java:51) at com.firebase.geofire.GeoQuery$1.onChildChanged(GeoQuery.java:79) at com.google.android.gms.internal.firebase_database.zzbt.zza(Unknown Source) at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source) at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5910) at java.lang.reflect.Method.invoke(Method.java) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

I am using this in production and having problem with my client, I am going to die :-( please help

ghost commented 6 years ago

You might still have an active query running when you are deleting. Remove the listeners before deleting location data. Also make sure that you are deleting the correct part of the tree, i.e. the key of the POI together with the children g and l. If you just delete the l part, this might also trigger your exception. Hard to diagnose without your code though.

morganchen12 commented 5 years ago

Fixed in 2577952.