Slide 1

Slide 1 text

Firebase Firestore : Presented by Michelle Wainaina Android Engineer @ Machini Technologies P. 01

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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") }

Slide 8

Slide 8 text

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) } }

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Thank P. 10 @mishwainaina Mitchell Wainaina You Contact