0

I have a situation with a lot of callbacks, specifically onCompleteListeners from Firebase Auth. First I am getting a Firebase Auth object using an SDK. However there is no other option but to use an onCompleteListener. Then I need to use another onCompleteListener to get the auth token for it. I have a use case with a suspend operator function that tries to do a Firebase realtime database call using a REST API. The problem is my code is getting really ugly.

What would be best would be to do something like you could do with Futures. I have seen async & await statements in other examples. What would be best is if I could use something like tags and awaits so I can await on some sections. If not I can use observers.

halfer
  • 19,824
  • 17
  • 99
  • 186
user1743524
  • 655
  • 1
  • 7
  • 14
  • Coroutines way is to almost never use callbacks and generally avoid futures - we use suspend functions instead of them. I don't have a project setup with Firebase right now to check the API, but if you need to perform two operations sequentially, it should be something like: `firebase.operation1().await(); firebase.operation2().await()`. https://stackoverflow.com/questions/64088407/how-to-use-async-await-coroutines-in-oncompletelistener-firebase – broot Jul 02 '23 at 08:39
  • use `suspendCancellableCoroutine{}` to convert Callback/Listener based code into suspendable coroutines – Jemshit Jul 09 '23 at 11:31

0 Answers0