0

I use Firebase and Swift.

There is an extreme case in which both completion handlers of an observer do not execute: Observer's completion handler not called, how to check in code and this:

Remove a user through dashboard, but the user is still logged in?

ref.child("users/\(user.uid)").observe(.value, with: { (snapshot) in
        print(snapshot)
    }, withCancel: { error in
        print(error)
    })

App does not stop at breakpoints on any prints.

How to check for this case programatically? In this particular case I need to do a logout for the user.

KENdi
  • 7,576
  • 2
  • 16
  • 31
Laura Calinoiu
  • 704
  • 1
  • 8
  • 24
  • Is child "\(user.uid)" exist in Firebase Realtime Database? – Hieu Dinh Aug 14 '17 at 06:58
  • No, in this particular case, `user.uid` does not exist anymore. But I can not get to any path either `users` or `deals`, but auth does not show this. – Laura Calinoiu Aug 14 '17 at 07:01
  • simply take a boolean flag variable and change its value inside completion handlers, if its value is unchanged after execution then you can conclude that completion handler did not get executed. then create you conditions according to the boolean variable – Rishabh Aug 14 '17 at 09:13
  • @Rishabh, would not work because completion handlers execute async. On this similar question, I have received an answer from Firebase: https://stackoverflow.com/questions/45668411/observers-completion-handler-not-called-how-to-check-in-code/45709324#45709324 – Laura Calinoiu Aug 16 '17 at 09:04
  • Possible duplicate of [Observer's completion handler not called, how to check in code](https://stackoverflow.com/questions/45668411/observers-completion-handler-not-called-how-to-check-in-code) – Laura Calinoiu Aug 16 '17 at 09:05
  • Can you elaborate on what the extreme case would be where the closure code is not called when there's a change in the /users/uid node? That should be addressed instead of band-aid'ing it. – Jay Aug 16 '17 at 12:36
  • @Jay, the extreme case is that I've removed the user from the Firebase Dashboard, but on the phone the user is still logged. I have understood that `withCancel` executes just when user needs authenticate but he has not rights. – Laura Calinoiu Aug 16 '17 at 13:05
  • @Jay, I don't know what "band-aid'ing" means. – Laura Calinoiu Aug 16 '17 at 13:06
  • It's a phrase used mean 'to add a temporary fix'. The meaning in this case is that what you are describing shouldn't ever happen so instead of treating the symptom, it would be better to identify the cause of the issue. – Jay Aug 16 '17 at 14:08
  • ...And I am not seeing that behavior. If I authenticate as a user, run the code in your question, delete the user and then write data to the users node, I get a snapshot printed in the console. So the completion handler is called in my case. Are you seeing something different or maybe I am testing it in a different way? – Jay Aug 16 '17 at 17:30
  • @Jay, thank you for being so helpful. When you try the code immediately, there is no problem, because the user still has a token. After some time (I've found out that 1 hour), the token of the user expires. Now this situation appears. – Laura Calinoiu Aug 17 '17 at 08:31
  • I've gone through the same process again; I ran my app, authenticated as a user then deleted the user via the Firebase console. I've now let the app sit for just over 5 hours and it still prints the snapshot in the console. Note that in my console, I am getting a notice from Firebase: * [Firebase/Auth][I-AUT000003] Token auto-refresh re-scheduled* so it clearly knows the token expired. For me, the code in the closure continues to work even after the user is deleted. – Jay Aug 18 '17 at 18:22
  • @Jay, What if the app is stopped? Stop the app, remove the user from Dashboard, and run it again after more than 1 hour. Would it print again the snapshot? – Laura Calinoiu Aug 19 '17 at 08:56
  • No, because at that point the user is no longer authenticated. – Jay Aug 19 '17 at 09:37
  • @Jay, this is the situation that I'm talking about. The snapshot is not printed, the withCancel is not executed, so nothing. On `with:` and `withCancel` completion handlers I have code, that does not execute anymore. :( And I don't know how reach in code this situation. – Laura Calinoiu Aug 19 '17 at 10:14
  • If the user is not authenticated, then they wouldn't be using the app and therefore that code would never be called. i.e. if the app stopped, the user would not be 'logged into the app' and wouldn't be observing any events or have access to any information. – Jay Aug 19 '17 at 10:16

0 Answers0