gpt4 book ai didi

java - 等待 Firestore 数据被检索

转载 作者:行者123 更新时间:2023-12-02 09:28:44 25 4
gpt4 key购买 nike

I need after inner foreach finish and add all data in list then send to interface

but when but this line " view.setOrders(orderList); " below " orderList.add(order); "

my code run okye but not that is not performance , I need best way to collection list then send to interface ..

public ListenerRegistration getOrders() {

view.showLoading();

ListenerRegistration listenerRegistration = refOrders.addSnapshotListener((queryDocumentSnapshots, e) -> {

view.hideLoading();

if (e != null) {
view.onErrorMessage(e.getMessage());
} else {
List<Order> orderList = new ArrayList<>();
for (QueryDocumentSnapshot snapshot : queryDocumentSnapshots) {

Order order = snapshot.toObject(Order.class);
order.setOrderId(snapshot.getId());

refUsers.document(order.getPhone()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {

if (task.isSuccessful()) {
User user = task.getResult().toObject(User.class);
order.setName(user.getName());
order.setAddress(user.getAddress());
orderList.add(order);

}


}
});
}

//Here Back List size = 0
view.setOrders(orderList);


}
});

return listenerRegistration;

}

最佳答案

get()方法是异步的,这意味着 CompletionListener 之后的代码将首先执行,然后在检索数据后执行 listener将被处决。

因此,要解决该问题,请添加以下行 view.setOrders(orderList);里面CompletionListener

if (task.isSuccessful()) {
User user = task.getResult().toObject(User.class);
order.setName(user.getName());
order.setAddress(user.getAddress());
orderList.add(order);
view.setOrders(orderList);

关于java - 等待 Firestore 数据被检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58141300/

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