Firebase document not deleting

270
January 09, 2019, at 1:50 PM

I am storing user FCM device tokens in Firebase. When the user logs in, the token is added to the user's profile like this:

        if (FirebaseAuth.getInstance().getCurrentUser()!=null) {
            FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
                @Override
                public void onSuccess(InstanceIdResult instanceIdResult) {
                    DeviceToken token = new DeviceToken(instanceIdResult.getToken());
                    CollectionReference deviceTokens = mUserCollection.document(mSignedInUserID).collection("device_tokens");
                    deviceTokens.document(token.getTokenID()).set(token);
                }
            });
        }

This works. However, I also want to delete that document when the user signs out. I am attempting to do so like this:

            FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
                @Override
                public void onSuccess(InstanceIdResult instanceIdResult) {
                    Log.d(TAG,instanceIdResult.getToken());
                    DocumentReference deactivatedToken = mUserCollection.document(mSignedInUserID).collection("device_tokens").document(instanceIdResult.getToken());
                    deactivatedToken.delete();
                    mAuth.signOut();
                    recreate();
                }
            });

Everything works in that method except for the actual deletion of that document, and the log statement confirms that the user's current ID matches the title of the document to be deleted. A simulation for a signed in user writing to that location returns allowed. What am I doing wrong?

Answer 1

Note that, with your code, the user is going to get signed out before the document is deleted. That's because the delete() is asynchronous (as well as all database operations), and returns immediately, before the work is complete. If I had to guess, I'd say that your authentication token is getting wiped out before the delete operation actually gets sent, and the delete is effectively acting as an unauthenticated user. So, what you should do is wait for the delete to complete for actually signing out the user. Use the Task returned by delete() to know when that finishes. It'll work the same way as the Task returned by getInstanceId().

Rent Charter Buses Company
READ ALSO
App Screen is blank when deployed through AppCenter

App Screen is blank when deployed through AppCenter

I have a XamarinForms app built for iOS and Android

177
load url in webview from notification while app is in foreground android

load url in webview from notification while app is in foreground android

I have a payload with my notification that sends a url so that when the user taps the notification it opens my webview to that urlI have this working great for when the app is in the background

268