section-engineering-education / engineering-education

“Section's Engineering Education (EngEd) Program is dedicated to offering a unique quality community experience for computer science university students."
Apache License 2.0
364 stars 890 forks source link

[Android] Using Kotlinx Coroutines in Firebase Authentication and Realtime Database in Android #5015

Closed joelkanyi closed 2 years ago

joelkanyi commented 2 years ago

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 the Background 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

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 and ViewModel and the Repository pattern.

References

Please list links to any published content/research that you intend to use to support/guide this article.

Conclusion

Kayere commented 2 years ago

Hello @JoelKanyi, this topic may have been covered here. How is yours going to be different?

joelkanyi commented 2 years ago

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

joelkanyi commented 2 years ago

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.

Kayere commented 2 years ago

Thank you for the information @JoelKanyi - topic approved.