Android Firestore任务,每个循环使用
我正试图在for循环中从Firestore检索文档引用(不遍历子集)。我需要等待循环完成,等待数据接收,成功后,将此数据提交到Firestore。目前,我的方法并不等待数据被接收,因为它是异步的 创建一个返回任务然后等待结果的方法可能是个好主意。建议Android Firestore任务,每个循环使用,android,android-asynctask,google-cloud-firestore,Android,Android Asynctask,Google Cloud Firestore,我正试图在for循环中从Firestore检索文档引用(不遍历子集)。我需要等待循环完成,等待数据接收,成功后,将此数据提交到Firestore。目前,我的方法并不等待数据被接收,因为它是异步的 创建一个返回任务然后等待结果的方法可能是个好主意。建议 ArrayList<String> documentPath = new ArrayList<>(); private void getDocumentRef() { try {
ArrayList<String> documentPath = new ArrayList<>();
private void getDocumentRef() {
try {
for (String path : documentPath) {
db.document(path).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful() && task.getResult() != null) {
if (task.getResult().exists()) {
references.add(task.getResult().getReference());
}
}
}
});
}
} catch (Exception e) {
}
}
ArrayList documentPath=new ArrayList();
私有void getDocumentRef(){
试一试{
用于(字符串路径:documentPath){
db.document(path).get().addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()&&task.getResult()!=null){
如果(task.getResult().exists()){
add(task.getResult().getReference());
}
}
}
});
}
}捕获(例外e){
}
}
要在完成对所选子集的请求后继续,您需要跟踪正在进行的查询数量和已完成的查询数量
在OnCompleteListener中创建成员变量以跟踪查询:
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
mCompleted++; // Update your member variable
if(task.isSuccessful(){
// Do something with your returned data
}else{
// The task failed
}
// Check if the last query has completed
if(mCompleted == numOfQueries){
mCompleted = 0; // Reset the completed queries if you might run this process again
// All of your queries have returned and you can now do something with the complete data set
}
}
@覆盖
未完成的公共void(@NonNull任务){
mCompleted++;//更新您的成员变量
if(task.issusccessful(){
//对返回的数据进行处理
}否则{
//任务失败了
}
//检查最后一个查询是否已完成
if(mCompleted==numofquerys){
mCompleted=0;//如果可能再次运行此进程,请重置已完成的查询
//所有查询都已返回,现在可以对完整的数据集执行某些操作
}
}
要在完成对所选子集的请求后继续,您需要跟踪正在进行的查询数量和已完成的查询数量
在OnCompleteListener中创建成员变量以跟踪查询:
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
mCompleted++; // Update your member variable
if(task.isSuccessful(){
// Do something with your returned data
}else{
// The task failed
}
// Check if the last query has completed
if(mCompleted == numOfQueries){
mCompleted = 0; // Reset the completed queries if you might run this process again
// All of your queries have returned and you can now do something with the complete data set
}
}
@覆盖
未完成的公共void(@NonNull任务){
mCompleted++;//更新您的成员变量
if(task.issusccessful(){
//对返回的数据进行处理
}否则{
//任务失败了
}
//检查最后一个查询是否已完成
if(mCompleted==numofquerys){
mCompleted=0;//如果可能再次运行此进程,请重置已完成的查询
//所有查询都已返回,现在可以对完整的数据集执行某些操作
}
}
如果我正确理解了您的问题,以下方法应该有效:
private void getDocumentRef(List<String> documentPaths) {
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (String path : documentPaths) {
tasks.add(db.document(path).get());
}
Task<List<DocumentSnapshot>> finalTask = Tasks.whenAllSuccess(tasks);
finalTask.addOnSuccessListener(new OnSuccessListener<List<DocumentSnapshot>>() {
@Override
public void onSuccess(List<DocumentSnapshot> documentSnapshots) {
/* This list contains all the retrieved document snapshots. Now iterate
through this list to get the document references.*/
for (DocumentSnapshot snapshot : documentSnapshots) {
references.add(snapshot.getReference());
}
/* Here u can do whatever u want with the list named references
because now it has references of all required documents. */
}
});
}
private void getDocumentRef(列出documentpath){
列表任务=新建ArrayList();
用于(字符串路径:DocumentPath){
tasks.add(db.document(path.get());
}
Task finalTask=任务。whenAllSuccess(任务);
finalTask.addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时公共作废(列表文档快照){
/*此列表包含所有检索到的文档快照。现在迭代
通过此列表获取文档引用*/
用于(DocumentSnapshot快照:DocumentSnapshot){
add(snapshot.getReference());
}
/*在这里,您可以对名为references的列表执行任何操作
因为现在它有了所有必要文件的参考*/
}
});
}
在这里,我们遍历提供的路径列表,创建一个单独的任务
,用于检索该路径上的文档,并将此任务添加到任务
列表中。然后,我们将此列表提供给任务。whenAllSuccess()
并创建一个名为finalTask
的新Task
。然后将OnSuccessListener
附加到finalTask
上,当所有提供的任务完成时,将调用其onSuccess()
方法是每个文档的DocumentSnapshot
列表。我们现在可以查看此列表并获得文档参考
希望有帮助!如果我正确理解了您的问题,以下方法应该有效:
private void getDocumentRef(List<String> documentPaths) {
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (String path : documentPaths) {
tasks.add(db.document(path).get());
}
Task<List<DocumentSnapshot>> finalTask = Tasks.whenAllSuccess(tasks);
finalTask.addOnSuccessListener(new OnSuccessListener<List<DocumentSnapshot>>() {
@Override
public void onSuccess(List<DocumentSnapshot> documentSnapshots) {
/* This list contains all the retrieved document snapshots. Now iterate
through this list to get the document references.*/
for (DocumentSnapshot snapshot : documentSnapshots) {
references.add(snapshot.getReference());
}
/* Here u can do whatever u want with the list named references
because now it has references of all required documents. */
}
});
}
private void getDocumentRef(列出documentpath){
列表任务=新建ArrayList();
用于(字符串路径:DocumentPath){
tasks.add(db.document(path.get());
}
Task finalTask=任务。whenAllSuccess(任务);
finalTask.addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时公共作废(列表文档快照){
/*此列表包含所有检索到的文档快照。现在迭代
通过此列表获取文档引用*/
用于(DocumentSnapshot快照:DocumentSnapshot){
add(snapshot.getReference());
}
/*在这里,您可以对名为references的列表执行任何操作
因为现在它有了所有必要文件的参考*/
}
});
}
在这里,我们遍历提供的路径列表,创建一个单独的任务
,用于检索该路径上的文档,并将此任务添加到任务
列表中。然后,我们将此列表提供给任务。whenAllSuccess()
并创建一个名为finalTask
的新Task
。然后将OnSuccessListener
附加到finalTask
上,当所有提供的任务完成时,将调用其onSuccess()
方法
是每个文档的DocumentSnapshot
列表。我们现在可以查看此列表并获得文档参考
希望对您有所帮助!您要查找的文档是否使用相同的c语言