use of com.google.firebase.firestore.WriteBatch in project FirebaseUI-Android by firebase.
the class FirestorePagingActivity method createItems.
@NonNull
private Task<Void> createItems() {
WriteBatch writeBatch = mFirestore.batch();
for (int i = 0; i < 250; i++) {
String title = "Item " + i;
String id = String.format(Locale.getDefault(), "item_%03d", i);
Item item = new Item(title, i);
writeBatch.set(mItemsCollection.document(id), item);
}
return writeBatch.commit();
}
use of com.google.firebase.firestore.WriteBatch in project snippets-android by firebase.
the class DocSnippets method writeBatch.
public void writeBatch() {
// [START write_batch]
// Get a new write batch
WriteBatch batch = db.batch();
// Set the value of 'NYC'
DocumentReference nycRef = db.collection("cities").document("NYC");
batch.set(nycRef, new City());
// Update the population of 'SF'
DocumentReference sfRef = db.collection("cities").document("SF");
batch.update(sfRef, "population", 1000000L);
// Delete the city 'LA'
DocumentReference laRef = db.collection("cities").document("LA");
batch.delete(laRef);
// Commit the batch
batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
// [END write_batch]
}
use of com.google.firebase.firestore.WriteBatch in project quickstart-android by firebase.
the class MainFragment method onAddItemsClicked.
private void onAddItemsClicked() {
// Add a bunch of random restaurants
WriteBatch batch = mFirestore.batch();
for (int i = 0; i < 10; i++) {
DocumentReference restRef = mFirestore.collection("restaurants").document();
// Create random restaurant / ratings
Restaurant randomRestaurant = RestaurantUtil.getRandom(requireContext());
List<Rating> randomRatings = RatingUtil.getRandomList(randomRestaurant.getNumRatings());
randomRestaurant.setAvgRating(RatingUtil.getAverageRating(randomRatings));
// Add restaurant
batch.set(restRef, randomRestaurant);
// Add ratings to subcollection
for (Rating rating : randomRatings) {
batch.set(restRef.collection("ratings").document(), rating);
}
}
batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Write batch succeeded.");
} else {
Log.w(TAG, "write batch failed.", task.getException());
}
}
});
}
use of com.google.firebase.firestore.WriteBatch in project firebase-android-sdk by firebase.
the class ConformanceRuntime method setup.
/**
* Initializes the database with the initial data.
*/
public void setup() throws InterruptedException, TimeoutException {
WriteBatch batch = firestore.batch();
for (TestCollection collection : initialData) {
CollectionReference ref = firestore.document(testDocument).collection(collection.path());
for (TestDocument document : collection.documents()) {
DocumentReference docRef = ref.document(document.id());
batch.set(docRef, document.fields());
logger.fine("Initializing document: " + docRef.getPath());
}
}
waitForBatch(batch);
}
use of com.google.firebase.firestore.WriteBatch in project firebase-android-sdk by firebase.
the class ConformanceRuntime method teardown.
/**
* Cleans up the database by removing the initialized data.
*/
public void teardown() throws InterruptedException, TimeoutException {
WriteBatch batch = firestore.batch();
for (TestCollection collection : initialData) {
CollectionReference ref = firestore.document(testDocument).collection(collection.path());
for (TestDocument document : collection.documents()) {
DocumentReference docRef = ref.document(document.id());
batch.delete(docRef);
logger.info("Removing document: " + docRef.getPath());
}
}
waitForBatch(batch);
}
Aggregations