gpt4 book ai didi

android - 事务Firestore后如何读取对象?

转载 作者:搜寻专家 更新时间:2023-11-01 09:27:52 24 4
gpt4 key购买 nike

如何在事务 Firestore 之后读取对象“数据”?为什么要为快照创建此对象?如何将快照直接复制到文档或如何读取对象“数据”?

enter image description here

交易:

mFirestoreRef = mFirestore.collection("item_decor").document(item_holly_id);
builder.setView(v)
.setTitle("Добавить элемент")
.setPositiveButton("Добавить", (dialog, which) ->
mFirestore.runTransaction((Transaction.Function<Void>) transaction -> {
DocumentSnapshot snapshot = transaction.get(mFirestoreRef);

long sumUpdateData = snapshot.getLong("sum");
int sumUpdate = (Integer) sum.getSelectedItem();

if(sumUpdateData >= sumUpdate) {
transaction.update(mFirestoreRef, "sum", sumUpdateData - sumUpdate);
DocumentReference addItemRef = mFirestore.collection("list_holly")
.document(holly.getSelectedItem().toString())
.collection("item_holly").document(snapshot.getId());

transaction.set(addItemRef, snapshot);
transaction.update(addItemRef, "sum", sumUpdate);
}

阅读:

mFirestore.collection("list_holly").document(title.getTitle()).collection("item_holly")
.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
String name = document.getString("name"); // Does not work
}
} else {
}
});

最佳答案

这不起作用,因为您缺少一个属性。要解决此问题,请将您的 get 调用更改为:

mFirestore.collection("list_holly")
.document(title.getTitle())
.collection("item_holly")
.get().addOnCompleteListener(task -> {
for (DocumentSnapshot document : task.getResult()) {
Map<String, Object> data = (Map<String, Object>) document.get("data");
for (Map.Entry<String, Object> entry : data.entrySet()) {
if (entry.getKey().equals("name")) {
String name = entry.getValue().toString();
Log.d("TAG", name);
}
}
}
});

输出将是:Decor

data 属性是一个Map,因此要实际获取名称,您只需遍历该Map。就是这样!

关于android - 事务Firestore后如何读取对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49490416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com