Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase Firestore: A COROUTINE STORY

Firebase Firestore: A COROUTINE STORY

Mitchell Wainaina

July 15, 2023
Tweet

More Decks by Mitchell Wainaina

Other Decks in Technology

Transcript

  1. What are Coroutines? Why should we use coroutines in Firestore?

    How to use coroutines in Firestore Best practices for coroutines in firestore What shall we talk about today? P. 02
  2. Concurrency design pattern that you can use on Android to

    simplify code that executes asynchronously. What are Coroutines P. 04 fun main() { println("Start") GlobalScope.launch { delay(1000) println("Coroutine: Executed after delay") } println("End") runBlocking { delay(2000) } } Start End Coroutine: Executed after delay
  3. Firestore is a cloud-based NoSQL document database provided by Google

    Cloud Platform (GCP). It is a powerful and scalable database designed for storing, synchronizing, and querying data for web, mobile, and server applications. What is Firestore P. 04 Bemefits OF Coroutines Scalable Realtime Data synchronization Flexible Data Model Easy to use
  4. There are many benefits of using coroutines when fetching data.

    Why should we use coroutines? P. 04 Bemefits OF Coroutines Simpler and concise way to write Asynchronous code Work well with suspend Functions Provide coroutine scopes that allow you to manage the lifecycle coroutines Offer built-in error handling mechanisms To avoid callback hell
  5. P. 06 db.collection("users") .get() .addOnSuccessListener { result -> for (document

    in result) { // Nested asynchronous operation db.collection("orders") .whereEqualTo("userId", document.id) .get() .addOnSuccessListener { orderResult -> for (orderDocument in orderResult) { Log.d(TAG, "Order ${orderDocument.id}") } // Another nested asynchronous operation db.collection("payments") .whereEqualTo("userId", document.id) .get() .addOnSuccessListener { paymentResult -> ... Code before Coroutines
  6. 01 Add the dependencies How to use Coroutines in Firestore

    P. 04 Bemefits OF Coroutines dependencies { // add coroutines for android implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9") // add firebase bill of material implementation("com.google.firebase:firebase-bom:*latest") implementation("com.google.firebase:firebase-firestore-ktx") }
  7. 02 Use coroutines when fetching data Code After Coroutines P.

    04 Bemefits OF Coroutines val scope = CoroutineScope(Dispatchers.Main) scope.launch { try { val result = withContext(Dispatchers.IO) { db.collection("users").get().await() } for (document in result) { Log.d(TAG, "${document.id} => ${document.data}") val orderResult = withContext(Dispatchers.IO) { db.collection("orders") .whereEqualTo("userId", document.id) .get() .await() } for (orderDocument in orderResult) { Log.d(TAG, "Order ${orderDocument.id}") } val paymentResult = withContext(Dispatchers.IO) { db.collection("payments") .whereEqualTo("userId", document.id) .get() .await() } for (paymentDocument in paymentResult) { Log.d(TAG, "Payment ${paymentDocument.id}") } } } catch (exception: Exception) { Log.w(TAG, "Error getting documents.", exception) } }
  8. P. 09 FAQs What is the difference between Realtime Database

    and Cloud Firestore? Firestore offers powerful querying with conditions, range queries, and efficient result ordering. Realtime Database has limited querying, retrieving entire branches. Complex queries may need data denormalization or restructuring. What are limitations of using Coroutines with Firebase Firestore? Some Firebase APIs do not yet support Coroutines