Я знаю, как получить идентификатор:
String cityId = db.collection("cities").document().getId();
db.collection("cities").document(cityId).set(city);
Но проще:
db.collection("cities").add(city);
Но как получить id? Не работает .add(city).getId(). Нет информации в документах.
Спасибо, нет как добавить?




How to get the id when call add in one step in Firestore?
Невозможно получить идентификатор документа за один шаг, как при использовании вызова document(). Чтобы решить эту проблему, вы должны добавить полноценного слушателя. Попробуй это:
db.collection("cities").add(city).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if (task.isSuccessful()) {
DocumentReference document = task.getResult();
if (document != null) {
String id = document.getId(); //Do what you need to do with the document id
Log.d(TAG, id);
}
}
}
});
Вы пробовали добавить полноценного слушателя?