在Android设备中同步调用firestore

问题描述 投票:0回答:1

我想通过Android设备更新firestore文档。代码是instrumented test的一部分,我要对Firestore进行同步调用以更新文档。

DocumentReference ref=db.collection("country").document("city")
                        .collection("Delhi").document("LajpatNagar");
Task<Void> temp=ref.update("isPinCodeAdded",true);
        while(!temp.isSuccessful()){    
        }

执行在无限的while loop中运行,任务永远不会成功。那么如何在android instrumented tests中向firestore进行同步调用?

android firebase google-cloud-firestore android-instrumentation
1个回答
1
投票

您可以阻止该线程并等待Firebase任务的结果

Tasks.await(YOUR_TASK)

在你的情况下,代码将是

Task<Void> temp = Tasks.await(ref.update("isPinCodeAdded",true));

有关更多详细信息,请参阅documentation

请记住,Android不会让你阻止主线程,所以如果你在主线程上,不要这样做。

© www.soinside.com 2019 - 2024. All rights reserved.