Kotlin Firestore example - CRUD Operations with RecyclerView | Android
Tags: kotlin, firestore, recyclerview, android
Published: 2021-04-04 09:54:35

https://grokonez.com/android/kotlin-firestore-example-crud-operations-with-recyclerview-android

Kotlin Firestore example - CRUD Operations with RecyclerView | Android

Cloud Firestore helps us store data in the cloud. It supports offline mode so our app will work fine (write, read, listen to, and query data) whether device has internet connection or not, it automatically fetches changes from our database to Firebase Server. We can structure data in our ways to improve querying and fetching capabilities. This tutorial show you an Android app that can do Firestore CRUD Operations with <code>RecyclerView</code>.

Related Post: <a href="https://grokonez.com/android/kotlin-firestore-example-crud-operations-with-firebaseui-firestorerecycleradapter-android">Kotlin Firestore example – CRUD Operations with FirebaseUI FirestoreRecyclerAdapter | Android</a>

<!--more-->
<h2>I. Technologies</h2>
- Android Studio 3
- Kotlin 1.2.0
- Firebase Firestore 11.8.0
<h2>II. Overview</h2>
<h3>1. Goal</h3>
We will build an Android App that supports showing, inserting, editing, deleting Notes from/to Cloud Firestore Database with <code>RecyclerView</code>:

<img src="https://grokonez.com/wp-content/uploads/2018/01/cloud-firestore-crud-demo-show-notes.png" alt="cloud-firestore-crud-demo-show-notes" width="657" height="514" class="alignnone size-full wp-image-10246" />

Firebase Console for Firestore will be like:

<img src="https://grokonez.com/wp-content/uploads/2018/01/cloud-firestore-crud-demo-console-show-notes.png" alt="cloud-firestore-crud-demo-console-show-notes" width="700" height="230" class="alignnone size-full wp-image-10250" />
<h3>2. Cloud Firestore</h3>
<h4>2.1 Add Firestore to Android App</h4>
- Create Android Project, then go to <a href="https://console.firebase.google.com" rel="nofollow" target="_blank">Firebase Console</a>, create Firebase Project:

<img src="https://grokonez.com/wp-content/uploads/2018/01/cloud-firestore-console-add-project.png" alt="cloud-firestore-console-add-project" width="510" height="483" class="alignnone size-full wp-image-10247" />

- When your app is created on Firebase Console, you will need to register Android Project. Now fill project package name to register your app:

<img src="https://grokonez.com/wp-content/uploads/2018/01/kotlin-cloud-firestore-add-firebase-project.png" alt="kotlin-cloud-firestore-add-firebase-project" width="683" height="569" class="alignnone size-full wp-image-10298" />

- Download <strong>google-service.json</strong> config file and follow instructions:

<img src="https://grokonez.com/wp-content/uploads/2018/01/cloud-firestore-console-add-firebase-android-json.png" alt="cloud-firestore-console-add-firebase-android-json" width="669" height="615" class="alignnone size-full wp-image-10251" />

- Add Service plugin for Gradle:

<img src="https://grokonez.com/wp-content/uploads/2018/01/cloud-firestore-console-add-firebase-gradle.png" alt="cloud-firestore-console-add-firebase-gradle" width="529" height="526" class="alignnone size-full wp-image-10252" />

- Open build.gradle (App-level), add dependency:
<pre><code class="language-java">
dependencies {
    // ...
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.google.firebase:firebase-firestore:11.8.0'
}

apply plugin: 'com.google.gms.google-services'
</code></pre>

- We also need Authentication to work with Firestore. To simplify this example without Authentication steps, we will allow anyone be able to read or write to the database:

<img src="https://grokonez.com/wp-content/uploads/2018/01/cloud-firestore-console-config-auth.png" alt="cloud-firestore-console-config-auth" width="452" height="287" class="alignnone size-full wp-image-10253" />

More at: https://grokonez.com/android/kotlin-firestore-example-crud-operations-with-recyclerview-android