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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ task("ktlint", type: JavaExec, group: "verification") {
classpath = configurations.ktlint
main = "com.github.shyiko.ktlint.Main"
args = [
"--format",
"--android",
"--reporter=plain",
"--reporter=checkstyle,output=${outputFile}",
Expand Down
2 changes: 1 addition & 1 deletion firestore/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
implementation "com.android.support:multidex:1.0.3"

// Firestore
implementation "com.google.firebase:firebase-firestore:18.1.0"
implementation "com.google.firebase:firebase-firestore:18.2.0"

// Firebase / Play Services
implementation "com.google.firebase:firebase-auth:16.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ public void updateDocumentArray() {
// [END update_document_array]
}

public void updateDocumentIncrement() {
// [START update_document_increment]
DocumentReference washingtonRef = db.collection("cities").document("DC");

// Atomically increment the population of the city by 50.
washingtonRef.update("population", FieldValue.increment(50));
// [END update_document_increment]
}

public void updateDocumentNested() {
// [START update_document_nested]
// Assume the document contains:
Expand Down Expand Up @@ -510,6 +519,9 @@ public void transactions() {
@Override
public Void apply(Transaction transaction) throws FirebaseFirestoreException {
DocumentSnapshot snapshot = transaction.get(sfDocRef);

// Note: this could be done without a transaction
// by updating the population using FieldValue.increment()
double newPopulation = snapshot.getDouble("population") + 1;
transaction.update(sfDocRef, "population", newPopulation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
Expand Down Expand Up @@ -73,18 +74,9 @@ public Task<Void> then(@NonNull Task<Void> task) throws Exception {
// [START increment_counter]
public Task<Void> incrementCounter(final DocumentReference ref, final int numShards) {
int shardId = (int) Math.floor(Math.random() * numShards);
final DocumentReference shardRef = ref.collection("shards").document(String.valueOf(shardId));

return db.runTransaction(new Transaction.Function<Void>() {
@Override
public Void apply(Transaction transaction) throws FirebaseFirestoreException {
Shard shard = transaction.get(shardRef).toObject(Shard.class);
shard.count += 1;

transaction.set(shardRef, shard);
return null;
}
});
DocumentReference shardRef = ref.collection("shards").document(String.valueOf(shardId));

return shardRef.update("count", FieldValue.increment(1));
}
// [END increment_counter]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ abstract class DocSnippets(val db: FirebaseFirestore) {
// [END update_document_array]
}

fun updateDocumentIncrement() {
// [START update_document_increment]
val washingtonRef = db.collection("cities").document("DC")

// Atomically incrememnt the population of the city by 50.
washingtonRef.update("population", FieldValue.increment(50))
// [END update_document_increment]
}

private fun updateDocumentNested() {
// [START update_document_nested]
// Assume the document contains:
Expand Down Expand Up @@ -379,6 +388,9 @@ abstract class DocSnippets(val db: FirebaseFirestore) {

db.runTransaction { transaction ->
val snapshot = transaction.get(sfDocRef)

// Note: this could be done without a transaction
// by updating the population using FieldValue.increment()
val newPopulation = snapshot.getDouble("population")!! + 1
transaction.update(sfDocRef, "population", newPopulation)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.google.example.firestore.kotlin
import com.google.android.gms.tasks.Task
import com.google.android.gms.tasks.Tasks
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.FieldValue
import com.google.firebase.firestore.FirebaseFirestore

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

return db.runTransaction { transaction ->
val shard = transaction.get(shardRef).toObject(Shard::class.java)
shard!!.count += 1

transaction.set(shardRef, shard!!)
null
}
return shardRef.update("count", FieldValue.increment(1))
}
// [END increment_counter]

Expand Down