Closed joelkanyi closed 2 years ago
Hello @JoelKanyi, this topic may have been covered here. How is yours going to be different?
In that article, the writer focuses on explaining Firebase Authentication with Email and Password. But the aim of my article is to showcase the use of Kotlin Coroutines when using Firebase. My article also focuses on using Coroutines in Realtime database. Thank you in advance @Kayere
In that article, there is the use of addOnCompleteListener
which in my article,
firebaseAuth.createUserWithEmailAndPassword(userEmail, userPassword)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
toast("created account successfully !")
sendEmailVerification()
startActivity(Intent(this, HomeActivity::class.java))
finish()
} else {
toast("failed to Authenticate !")
}
}
I will expound on how to replace those interfaces with Coroutines.
Thank you for the information @JoelKanyi - topic approved.
Using Kotlinx Coroutines in Firebase Authentication and Realtime Database in Android
Proposed article introduction
Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. Also, the Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. All this two makes the work of Android Developers easier because they don't want to write on their own backend.
In Android, while making network calls such as user Authentication, sending or querying data from Firebase Database; it is advisable that you don't perform such tasks on the
Main Thread
. You should do such tasks on theBackground Thread
and then update the UI accordingly.In Kotlin, a Coroutine is a concurrency design pattern that you can use on Android to simplify code that executes asynchronously. On Android, Coroutines helps manage long-running tasks that might otherwise block the main thread and cause your app to become unresponsive.
When we perform Firebase tasks using
Coroutines
, our code looks cleaner with no boilerplate code, making it more readable and understandable, and in the long run, it improves app productivity.Key takeaways
OnCompleteListeners
Interfaces withawait
function.SafeCall
Extension function for making network calls.Article quality
My article will be beginner friendly, making it easier for readers to understand the concepts. Also most of the articles online don't use Jetpack Components, so in my article, I will use some concepts of Jetpack Components such us
Livedata
andViewModel
and theRepository
pattern.References
Please list links to any published content/research that you intend to use to support/guide this article.
Conclusion