|
| 1 | +# Firestore Android SDK Usage Guide |
| 2 | + |
| 3 | +This guide uses **Kotlin** and **KTX extensions**, which correspond to the modern Android development standards. |
| 4 | + |
| 5 | +## Initialization |
| 6 | + |
| 7 | +```kotlin |
| 8 | +// In your Activity or Application class |
| 9 | +import com.google.firebase.firestore.ktx.firestore |
| 10 | +import com.google.firebase.firestore.SetOptions |
| 11 | +import com.google.firebase.ktx.Firebase |
| 12 | + |
| 13 | +val db = Firebase.firestore |
| 14 | + |
| 15 | +// Connect to Emulator |
| 16 | +// Use 10.0.2.2 to access localhost from the Android Emulator |
| 17 | +if (BuildConfig.DEBUG) { |
| 18 | + db.useEmulator("10.0.2.2", 8080) |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +## Writing Data |
| 23 | + |
| 24 | +### Set a Document (`set`) |
| 25 | +Creates or overwrites a document. |
| 26 | + |
| 27 | +```kotlin |
| 28 | +val city = hashMapOf( |
| 29 | + "name" to "Los Angeles", |
| 30 | + "state" to "CA", |
| 31 | + "country" to "USA" |
| 32 | +) |
| 33 | + |
| 34 | +db.collection("cities").document("LA") |
| 35 | + .set(city) |
| 36 | + .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully written!") } |
| 37 | + .addOnFailureListener { e -> Log.w(TAG, "Error writing document", e) } |
| 38 | + |
| 39 | +// Merge |
| 40 | +db.collection("cities").document("LA") |
| 41 | + .set(mapOf("population" to 3900000), SetOptions.merge()) |
| 42 | +``` |
| 43 | + |
| 44 | +### Add a Document with Auto-ID (`add`) |
| 45 | + |
| 46 | +```kotlin |
| 47 | +val data = hashMapOf( |
| 48 | + "name" to "Tokyo", |
| 49 | + "country" to "Japan" |
| 50 | +) |
| 51 | + |
| 52 | +db.collection("cities") |
| 53 | + .add(data) |
| 54 | + .addOnSuccessListener { documentReference -> |
| 55 | + Log.d(TAG, "DocumentSnapshot written with ID: ${documentReference.id}") |
| 56 | + } |
| 57 | +``` |
| 58 | + |
| 59 | +### Update a Document (`update`) |
| 60 | + |
| 61 | +```kotlin |
| 62 | +val laRef = db.collection("cities").document("LA") |
| 63 | + |
| 64 | +laRef.update("capital", true) |
| 65 | + .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully updated!") } |
| 66 | +``` |
| 67 | + |
| 68 | +### Transactions |
| 69 | +Atomic read-modify-write. |
| 70 | + |
| 71 | +```kotlin |
| 72 | +db.runTransaction { transaction -> |
| 73 | + val sfDocRef = db.collection("cities").document("SF") |
| 74 | + val snapshot = transaction.get(sfDocRef) |
| 75 | + |
| 76 | + // Note: You can also use FieldValue.increment() for simple counters |
| 77 | + val newPopulation = (snapshot.getDouble("population") ?: 0.0) + 1 |
| 78 | + transaction.update(sfDocRef, "population", newPopulation) |
| 79 | + |
| 80 | + // Success |
| 81 | + null |
| 82 | +}.addOnSuccessListener { Log.d(TAG, "Transaction success!") } |
| 83 | + .addOnFailureListener { e -> Log.w(TAG, "Transaction failure.", e) } |
| 84 | +``` |
| 85 | + |
| 86 | +## Reading Data |
| 87 | + |
| 88 | +### Get a Single Document (`get`) |
| 89 | + |
| 90 | +```kotlin |
| 91 | +val docRef = db.collection("cities").document("SF") |
| 92 | + |
| 93 | +docRef.get().addOnSuccessListener { document -> |
| 94 | + if (document != null && document.exists()) { |
| 95 | + Log.d(TAG, "DocumentSnapshot data: ${document.data}") |
| 96 | + } else { |
| 97 | + Log.d(TAG, "No such document") |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Get Multiple Documents (`get`) |
| 103 | + |
| 104 | +```kotlin |
| 105 | +db.collection("cities") |
| 106 | + .get() |
| 107 | + .addOnSuccessListener { result -> |
| 108 | + for (document in result) { |
| 109 | + Log.d(TAG, "${document.id} => ${document.data}") |
| 110 | + } |
| 111 | + } |
| 112 | +``` |
| 113 | + |
| 114 | +## Realtime Updates |
| 115 | + |
| 116 | +### Listen to Changes (`addSnapshotListener`) |
| 117 | + |
| 118 | +```kotlin |
| 119 | +val docRef = db.collection("cities").document("SF") |
| 120 | + |
| 121 | +docRef.addSnapshotListener { snapshot, e -> |
| 122 | + if (e != null) { |
| 123 | + Log.w(TAG, "Listen failed.", e) |
| 124 | + return@addSnapshotListener |
| 125 | + } |
| 126 | + |
| 127 | + if (snapshot != null && snapshot.exists()) { |
| 128 | + val source = if (snapshot.metadata.hasPendingWrites()) "Local" else "Server" |
| 129 | + Log.d(TAG, "$source data: ${snapshot.data}") |
| 130 | + } else { |
| 131 | + Log.d(TAG, "Current data: null") |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +## Queries |
| 137 | + |
| 138 | +### Simple and Compound |
| 139 | +Note: Compound queries on different fields require an index. |
| 140 | + |
| 141 | +```kotlin |
| 142 | +// Simple |
| 143 | +db.collection("cities").whereEqualTo("state", "CA") |
| 144 | + |
| 145 | +// Compound (AND) |
| 146 | +db.collection("cities") |
| 147 | + .whereEqualTo("state", "CA") |
| 148 | + .whereGreaterThan("population", 1000000) |
| 149 | +``` |
| 150 | + |
| 151 | +### Order and Limit |
| 152 | + |
| 153 | +```kotlin |
| 154 | +db.collection("cities") |
| 155 | + .orderBy("name", Query.Direction.ASCENDING) |
| 156 | + .limit(3) |
| 157 | +``` |
0 commit comments