我是一名本科生,试图制作一个使用Firebase的Android应用程序。请让我知道这个问题是否模棱两可/需要更多上下文信息才能获得完整答案。如果我似乎误解了本文中的一些概念,请告诉我
客观
将异步任务的结果用作另一个异步任务的参数。
上下文
我想使用过滤器从Firestore检索特定文档,然后将生成的文档用作whereArrayContains()方法的参数。具体来说,我首先要找到DocumentA,从中获取一些值,然后将它们存储在意图中以供以后处理。然后,使用对DocumentA的引用,我需要在collectionB中搜索并尝试查找一些文档(如果存在),将其称为DocumentB,该文档在“ documents”数组中包含DocumentA的引用。然后,根据是否找到DocumentB,将bool存放在意图中。现在我有了所需的一切,最后我从嵌套onCompleteListener()中的意图开始了活动。我最初的尝试似乎并未开始活动。这是我出于可读性原因而摘要的内容:
collectionA.whereEqualTo("key1", "val1")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
intent.putExtra("key2", (int) doc.getData().get("val2"));
for(QueryDocumentSnapshot doc : task.getResult()) {
intent.putExtra("key2", (int) doc.getData().get("val2"));
collectionB.whereArrayContains("documents", doc.getReference())
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for(QueryDocumentSnapshot doc : task.getResult()){
intent.putExtra("key3", true);
}
}
startActivity(intent);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Handle collectionB failure
}
});
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Handle collectionA failure
}
});
其他信息
我很抱歉如果语法不正确
CollectionA,CollectionB,DocumentA和DocumentB定义如下:
CollectionA: {
DocumentA: {
key1: "val1" // type String
key2: "val2" // type String
}
// all other documents in this collection are defined similarly
}
CollectionB: {
DocumentB: {
// array of document references
documents: [CollectionA/DocumentC, CollectionG/DocumentH, ...]
}
// all other documents in this collection are defined similarly
}
我做错了什么,我能做得更好?是否有内置函数来处理这种特殊情况?