Firestore
Firestore is a flexible, NoSQL cloud database from Firebase. It allows you to store and retrieve data in a document-oriented format, making it ideal for building scalable and dynamic mobile applications. Firestore offers offline capabilities, real-time data synchronization, and powerful querying features.
Prerequisites
Firebase Setup
Before continuing, ensure you have Firebase set up for your project. If not, follow the instructions in our guide.
Creating a Firestore database
- Navigate to the Firebase console .
- In the sidebar menu, locate the Build section and click on it.
- Within the Build section, find Firestore Database
- Click on Create database and follow the steps
Usage
Dependencies
Within your app
module’s build.gradle.kts
file, incorporate the following dependencies:
implementation(project(":core:firebase"))
implementation(project(":storage:firestore"))
Example
The FirestoreRepository
class serves as a foundation for building your custom Firestore repositories. You can extend this class to create specific repositories for your data models.
class CityRepository: FirestoreRepository()
The FirestoreRepository
constructor accepts an optional FirebaseFirestoreSettings
parameter, allowing you to customize settings for your Firestore instance.
It also provides a firestore
property within the repository, offering access to the Firestore instance for performing queries.
When performing queries on Firestore, the get()
method returns a Task
object. To simplify handling these asynchronous tasks, we recommend using the executeTask
method provided by the :core:firebase module. This method executes the task and manages the results, notifying you through the FirebaseCallback
interface.
Additional features
- Pagination: Utilize the
paginate
method to efficiently handle large datasets by retrieving data in smaller chunks. Instead of using the regularFirebaseCallback
it usesFirebasePaginationCallback
which has anext
field in theonSuccess
. This field returns theQuery
object for the next page of data. - Real-time Data: Leverage the
realtime
method to establish real-time data subscriptions, keeping your app updated with any changes in Firestore.
More information on Firestore can be found in the official documentation .