Search in sources :

Example 1 with WriteBatch

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();
}
Also used : MenuItem(android.view.MenuItem) WriteBatch(com.google.firebase.firestore.WriteBatch) NonNull(androidx.annotation.NonNull)

Example 2 with WriteBatch

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]
}
Also used : WriteBatch(com.google.firebase.firestore.WriteBatch) DocumentReference(com.google.firebase.firestore.DocumentReference)

Example 3 with WriteBatch

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());
            }
        }
    });
}
Also used : Restaurant(com.google.firebase.example.fireeats.java.model.Restaurant) Rating(com.google.firebase.example.fireeats.java.model.Rating) WriteBatch(com.google.firebase.firestore.WriteBatch) DocumentReference(com.google.firebase.firestore.DocumentReference)

Example 4 with WriteBatch

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);
}
Also used : WriteBatch(com.google.firebase.firestore.WriteBatch) CollectionReference(com.google.firebase.firestore.CollectionReference) DocumentReference(com.google.firebase.firestore.DocumentReference)

Example 5 with WriteBatch

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);
}
Also used : WriteBatch(com.google.firebase.firestore.WriteBatch) CollectionReference(com.google.firebase.firestore.CollectionReference) DocumentReference(com.google.firebase.firestore.DocumentReference)

Aggregations

WriteBatch (com.google.firebase.firestore.WriteBatch)6 DocumentReference (com.google.firebase.firestore.DocumentReference)4 CollectionReference (com.google.firebase.firestore.CollectionReference)2 MenuItem (android.view.MenuItem)1 NonNull (androidx.annotation.NonNull)1 WorkerThread (androidx.annotation.WorkerThread)1 Rating (com.google.firebase.example.fireeats.java.model.Rating)1 Restaurant (com.google.firebase.example.fireeats.java.model.Restaurant)1 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)1 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)1