为什么命令 transaction.update 在 carrelloAttuale.

我正在尝试从云 Firestore 的文档中获取产品,然后将该产品放入购物车。当我(成功)阅读该产品时,我尝试将其放入在外部声明的数组列表中,但除非我将 final 放入变量,否则它不起作用。这样做,当我运行下面的代码时,我成功地检索了数据,但是操作 carrelloAttuale.prodotti.add(prod) 是在命令 transaction.update() 之后执行的,因此更新不会上传与开始时没有什么不同.


//prendo l'utente

                FirebaseAuth auth= FirebaseAuth.getInstance();


                //mi salvo il codice del prodotto scannerizzato

                final String codiceProdottoScannerizzato=String.valueOf(intentData);

                final FirebaseFirestore db = FirebaseFirestore.getInstance();

                final DocumentReference docRef = db.collection("carrelli").document(auth.getUid());

                final DocumentReference docrefprodotti = db.collection("prodotti").document(codiceProdottoScannerizzato);

                db.runTransaction(new Transaction.Function<Void>() {

                    @Override

                    public Void apply(Transaction transaction) throws FirebaseFirestoreException {

                        DocumentSnapshot snapshot = transaction.get(docRef);

                        final Carrello carrelloAttuale = snapshot.toObject(Carrello.class);

我希望在调试日志中的 carrelloAttuale.prodotti.add(prod) 之后执行命令更新标签的顺序是:CARRELLO FB:0 PRODOTTO:Nome:latte


呼啦一阵风
浏览 103回答 2
2回答

白板的微信

数据从 Firestore 异步加载,因为它可能必须从服务器检索。为防止阻止应用程序,主要代码在检索数据时继续执行。然后,当数据可用时,您onComplete会被调用。这意味着任何需要数据中的数据的代码都必须在方法内部onComplete,或者从那里调用。所以像:docrefprodotti.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onComplete(@NonNull Task<DocumentSnapshot> task) {&nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DocumentSnapshot document = task.getResult();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (document.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Prodotti prod=document.toObject(Prodotti.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prod.id=codiceProdottoScannerizzato;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prod.totalePezziCarrello=1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; carrelloAttuale.prodotti.add(prod);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "PRODOTTO: " + prod.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "No such document");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "get failed with ", task.getException());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "CARRELLO FB: " + carrelloAttuale.size());&nbsp; &nbsp; &nbsp; &nbsp; transaction.update(docRef, "prodotti", carrelloAttuale.getProdotti());&nbsp; &nbsp; }});

墨色风雨

“命令更新”在调用“carrelloAttuale.prodotti.add(prod)”之前执行,因为该onComplete()方法具有异步行为并立即返回。这意味着在数据库更新操作完成之后的某个时间之后,侦听器才会被调用。无法保证需要多长时间。根据您的连接速度和状态,更新操作可能需要几百毫秒到几秒才能完成。如果您想对该数据使用某些逻辑,则必须等到异步 Firebase 数据库操作完成。这意味着您只能使用prod侦听器回调本身内部的对象。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java