Cloud Firestore with Jetpack Compose — Android
The Cloud Firestore service of Firebase helps to add the database to your app with just a few lines of code and the best part is that this service is also available for iOS and web. Let’s see with Jetpack Compose how can we get started using JetFirestore.
Add Dependencies
The first thing before I talked about the required dependency is to switch over to the Firebase console and create a project so as to link your android project with Firebase.
implementation platform('com.google.firebase:firebase-bom:30.1.0')
implementation 'com.google.firebase:firebase-firestore'
implementation 'com.github.raipankaj:JetFirestore:1.0.3'
JetFirestore
The heart of Jetpack Compose is the observable state and if you have used Firestore APIs before then you might be aware that it offers greater support for callback-based APIs. Now with JetFirestore, you can avoid the callback-based APIs calls and instead use it just like the way you use any other composable.
Now let’s talk about the individual parameters as each of them is associated with some features.
path: Here you will have to provide the reference path based on collection & document something like this
//When you just have a single collection reference
path = { collection("books") }
//When you have a reference till document
path = { collection("books").document("store") }
limitOnSingleTimeCollectionFetch: This will be helpful to limit the number of fetches from the database, it will only be useful when you need pagination support.
queryOnCollection: Doing a query is the most common task when you add a filter concept in your app so with this parameter you can define the various query’s constraints
queryOnCollection = { orderBy("cost", Query.Direction.DESCENDING) },
Firestore offers two ways to fetch data from the database. One is to fetch as and when called and another one is not just to fetch but also to attach an observer so that if any content changes with respect to the referenced path then it will automatically pull the latest data from the database.
onSingleTimeCollectionFetch: The content will be fetched only for a single time and it doesn’t attach any sort of observer. So if you want to fetch data when users launch the screen and do not want to update content in real-time then this will be helpful for you.
onSingleTimeCollectionFetch = { values, exception ->
//When all documents are fetched
//booksList = values.getListOfObjects() //When documents are fetched based on limit
booksList = booksList + values.getListOfObjects()
}
When your call to the database is done then lambda will get triggered automatically and you may update the state so as to update content on UI.
onSingleTimeDocumentFetch: This is similar to the previous one but instead of providing the list of collections here it will provide the document itself.
onRealtimeCollectionFetch: Here unlike the onSingleTimeCollectionFetch it will first fetch the list of documents and then also set an active observer that will listen for the updates with respect to the reference path.
onRealtimeCollectionFetch = { values, exception ->
booksList = values.getListOfObjects()
}
onRealtimeDocumentFetch: Here it will fetch the document and also sets an observer that will listen for any changes in that particular document. So if you want to update UI based on the content changes in a particular document then this might be useful for you.
content: The content lambda is of composable extension which means you can have your composable that will display the content pulled from the database at this place.
Well this library is open-source and here is the link for it
https://github.com/raipankaj/JetFirestore
In the next article, we will see how Cloud Storage can be used in the app built with Jetpack Compose.