in firestore/app/src/main/java/com/google/firebase/example/fireeats/java/MainFragment.java [274:304]
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());
}
}
});
}