Android中异步异步问题

时间:2019-04-09 08:17:58

标签: java android firebase asynchronous google-cloud-firestore

我有问题,因为在另一个异步事件中有异步事件。我想等待upload.setPhone的结果,然后做以下几行

upload.setName(doc.getString("text"));
                        upload.setImageUrl(doc.getString("photo"));...

” 我读了How to return a DocumentSnapShot as a result of a method?,但嵌套事件对我来说是个问题:/现在我得到这样的结果Logcat

   databaseReference.collection("advertisements")
            .whereEqualTo("userId",FirebaseAuth.getInstance().getUid())
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@javax.annotation.Nullable QuerySnapshot snapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                    if (e != null) {
                        System.err.println("Listen failed:" + e);
                        return;
                    }
                    Log.d(TAG, "Zero" );
                    for (final DocumentSnapshot doc : snapshots) {
                        Log.d(TAG, "First");

                        final Upload upload = new Upload();
                        databaseReferenceUsers.collection("users").document(doc.getString("userId")).addSnapshotListener(new EventListener<DocumentSnapshot>() {
                            @Override
                            public void onEvent(@javax.annotation.Nullable DocumentSnapshot documentSnapshot, @javax.annotation.Nullable FirebaseFirestoreException e) {
                                upload.setPhone(Integer.toString(documentSnapshot.getLong("phone").intValue()));
                                Log.d(TAG, "Second" );
                                Log.d(TAG, "Third" );
                                upload.setName(doc.getString("text"));
                                upload.setImageUrl(doc.getString("photo"));
                                upload.setKind(doc.getString("kind"));
                                upload.setType(doc.getString("type"));
                                upload.setPrice(doc.getString("price") + "zł");

                                upload.setVoivodeship(doc.getString("voivodeship"));
                                upload.setVariety(doc.getString("variety"));


                                mUploads.add(upload);
                                Log.d(TAG, "fourth" );
                            }
                        });


                    }
                    Log.d(TAG, "fifth" );
                    imageAdapter = new ImageAdapter(MyAdvertisementsActivity.this,mUploads);
                    recyclerView.setAdapter(imageAdapter);
                    progressCircle.setVisibility(View.INVISIBLE);

                }
            });

0 个答案:

没有答案