Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b3dce5

Browse files
authored
Firestore Numeric Transforms (firebase#91)
1 parent 497e0b4 commit 7b3dce5

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ task("ktlint", type: JavaExec, group: "verification") {
6060
classpath = configurations.ktlint
6161
main = "com.github.shyiko.ktlint.Main"
6262
args = [
63+
"--format",
6364
"--android",
6465
"--reporter=plain",
6566
"--reporter=checkstyle,output=${outputFile}",

firestore/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dependencies {
4040
implementation "com.android.support:multidex:1.0.3"
4141

4242
// Firestore
43-
implementation "com.google.firebase:firebase-firestore:18.1.0"
43+
implementation "com.google.firebase:firebase-firestore:18.2.0"
4444

4545
// Firebase / Play Services
4646
implementation "com.google.firebase:firebase-auth:16.2.0"

firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,15 @@ public void updateDocumentArray() {
454454
// [END update_document_array]
455455
}
456456

457+
public void updateDocumentIncrement() {
458+
// [START update_document_increment]
459+
DocumentReference washingtonRef = db.collection("cities").document("DC");
460+
461+
// Atomically increment the population of the city by 50.
462+
washingtonRef.update("population", FieldValue.increment(50));
463+
// [END update_document_increment]
464+
}
465+
457466
public void updateDocumentNested() {
458467
// [START update_document_nested]
459468
// Assume the document contains:
@@ -510,6 +519,9 @@ public void transactions() {
510519
@Override
511520
public Void apply(Transaction transaction) throws FirebaseFirestoreException {
512521
DocumentSnapshot snapshot = transaction.get(sfDocRef);
522+
523+
// Note: this could be done without a transaction
524+
// by updating the population using FieldValue.increment()
513525
double newPopulation = snapshot.getDouble("population") + 1;
514526
transaction.update(sfDocRef, "population", newPopulation);
515527

firestore/app/src/main/java/com/google/example/firestore/SolutionCounters.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.android.gms.tasks.Tasks;
88
import com.google.firebase.firestore.DocumentReference;
99
import com.google.firebase.firestore.DocumentSnapshot;
10+
import com.google.firebase.firestore.FieldValue;
1011
import com.google.firebase.firestore.FirebaseFirestore;
1112
import com.google.firebase.firestore.FirebaseFirestoreException;
1213
import com.google.firebase.firestore.QuerySnapshot;
@@ -73,18 +74,9 @@ public Task<Void> then(@NonNull Task<Void> task) throws Exception {
7374
// [START increment_counter]
7475
public Task<Void> incrementCounter(final DocumentReference ref, final int numShards) {
7576
int shardId = (int) Math.floor(Math.random() * numShards);
76-
final DocumentReference shardRef = ref.collection("shards").document(String.valueOf(shardId));
77-
78-
return db.runTransaction(new Transaction.Function<Void>() {
79-
@Override
80-
public Void apply(Transaction transaction) throws FirebaseFirestoreException {
81-
Shard shard = transaction.get(shardRef).toObject(Shard.class);
82-
shard.count += 1;
83-
84-
transaction.set(shardRef, shard);
85-
return null;
86-
}
87-
});
77+
DocumentReference shardRef = ref.collection("shards").document(String.valueOf(shardId));
78+
79+
return shardRef.update("count", FieldValue.increment(1));
8880
}
8981
// [END increment_counter]
9082

firestore/app/src/main/java/com/google/example/firestore/kotlin/DocSnippets.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ abstract class DocSnippets(val db: FirebaseFirestore) {
335335
// [END update_document_array]
336336
}
337337

338+
fun updateDocumentIncrement() {
339+
// [START update_document_increment]
340+
val washingtonRef = db.collection("cities").document("DC")
341+
342+
// Atomically incrememnt the population of the city by 50.
343+
washingtonRef.update("population", FieldValue.increment(50))
344+
// [END update_document_increment]
345+
}
346+
338347
private fun updateDocumentNested() {
339348
// [START update_document_nested]
340349
// Assume the document contains:
@@ -379,6 +388,9 @@ abstract class DocSnippets(val db: FirebaseFirestore) {
379388

380389
db.runTransaction { transaction ->
381390
val snapshot = transaction.get(sfDocRef)
391+
392+
// Note: this could be done without a transaction
393+
// by updating the population using FieldValue.increment()
382394
val newPopulation = snapshot.getDouble("population")!! + 1
383395
transaction.update(sfDocRef, "population", newPopulation)
384396

firestore/app/src/main/java/com/google/example/firestore/kotlin/SolutionCounters.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.google.example.firestore.kotlin
33
import com.google.android.gms.tasks.Task
44
import com.google.android.gms.tasks.Tasks
55
import com.google.firebase.firestore.DocumentReference
6+
import com.google.firebase.firestore.FieldValue
67
import com.google.firebase.firestore.FirebaseFirestore
78

89
/**
@@ -48,13 +49,7 @@ class SolutionCounters(val db: FirebaseFirestore) {
4849
val shardId = Math.floor(Math.random() * numShards).toInt()
4950
val shardRef = ref.collection("shards").document(shardId.toString())
5051

51-
return db.runTransaction { transaction ->
52-
val shard = transaction.get(shardRef).toObject(Shard::class.java)
53-
shard!!.count += 1
54-
55-
transaction.set(shardRef, shard!!)
56-
null
57-
}
52+
return shardRef.update("count", FieldValue.increment(1))
5853
}
5954
// [END increment_counter]
6055

0 commit comments

Comments
 (0)