Skip to content

Commit 164736d

Browse files
committed
Correct batched write not to overwrite the level and have only one at the end, but to add all levels to it. And add userId to Level also.
1 parent c21afc1 commit 164736d

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

app/src/main/java/com/tiansirk/countryquiz/data/Repository.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,15 @@ public void onFailure(@NonNull Exception e) {
137137
});
138138
}
139139

140-
public Task<Void> saveLevels(String userId, List<? extends TEntity> entities) {
140+
public Task<Void> saveLevels(String parentDocumentId, List<? extends TEntity> entities) {
141141
WriteBatch batch = db.batch();
142-
DocumentReference userDocumentReference = collectionReference.document(userId);
143-
DocumentReference levelDocumentReference = userDocumentReference.collection(LEVELS_SUBCOLLECTION_NAME).document();
142+
DocumentReference userDocumentReference = collectionReference.document(parentDocumentId);
143+
144144
for(TEntity tEntity : entities){
145+
if(Level.class.isInstance(tEntity)){
146+
((Level)tEntity).setUserId(parentDocumentId);
147+
}
148+
DocumentReference levelDocumentReference = userDocumentReference.collection(LEVELS_SUBCOLLECTION_NAME).document();
145149
batch.set(levelDocumentReference, tEntity);
146150
}
147151
return batch.commit().addOnFailureListener(activity, new OnFailureListener() {

0 commit comments

Comments
 (0)