栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Firestore连接执行线程时出现问题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Firestore连接执行线程时出现问题

问题不是由多线程引起的,而是由Firebase异步加载数据引起的。等到您的

updateUI
函数查看的值时
aa
onComplete
尚未运行。

通过放置一些放置良好的日志记录语句,这最容易看到:

System.out.println("Before attaching listener");docRef.get().addonCompleteListener(new OnCompleteListener<documentSnapshot>() {    @Override    public void onComplete(@NonNull Task<documentSnapshot> task) {        System.out.println("Got document");    }});System.out.println("After attaching listener");

当您运行此代码时,它会打印

附加监听器之前

附加监听器后

得到文件

这可能不是您所期望的,但恰恰说明了检查

aa
时为什么未修改的原因
updateUI
。该文件尚未从Firestore中读取,因此
onComplete
尚未运行。

此解决方案是移动从数据库中需要的数据的所有码

onComplete
方法。在这种情况下,最简单的方法是:

public void userExists(String uid) {  FirebaseFirestore db = FirebaseFirestore.getInstance();  documentReference docRef = db.collection("users").document(uid);  docRef.get().addonCompleteListener(new OnCompleteListener<documentSnapshot>() {    @Override    public void onComplete(@NonNull Task<documentSnapshot> task) {        if (task.isSuccessful()) { documentSnapshot documentSnapshot = task.getResult(); if (documentSnapshot.exists()) {     //Fill layout with the user data and the user linked document data     //USER DATA     txvNombre=findViewById(R.id.nombrePerfil);     txvNombre.setText(user.getDisplayName());     imvAvatar=findViewById(R.id.imvVistaPerfilAvatar);     Picasso.with(VistaPerfilActivity.this)  .load(user.getPhotoUrl())  .resize(500,500)  .centerCrop()  .into(imvAvatar);     //HERE GOES THE document DATA }        }    }  });}

现在,需要文档的代码仅在文档实际可用后才运行。这将起作用,但是确实会使该

userExists
函数的可重用性降低。如果要解决此问题,可以将回调传递
userExists
该回调中,然后在加载文档后调用该回调。

public interface UserExistsCallback {  void onCallback(boolean isExisting);}

并将其

userExists
用作:

public void userExists(String uid, final UserExistsCallback callback) {  FirebaseFirestore db = FirebaseFirestore.getInstance();  documentReference docRef = db.collection("users").document(uid);  docRef.get().addonCompleteListener(new OnCompleteListener<documentSnapshot>() {    @Override    public void onComplete(@NonNull Task<documentSnapshot> task) {        boolean userExists = false;        if (task.isSuccessful()) { documentSnapshot documentSnapshot = task.getResult(); userExists = documentSnapshot.exists();        }        callback.onCallback(userExists);    }  });}

然后使用以下命令调用它

updateUI

if (user != null) {  userExists(user.getUid(), new UserExistsCallback() {    public void onCallback(boolean isExisting) {      if(isExisting){        //Fill layout with the user data and the user linked document data        //USER DATA        txvNombre=findViewById(R.id.nombrePerfil);        txvNombre.setText(user.getDisplayName());        imvAvatar=findViewById(R.id.imvVistaPerfilAvatar);        Picasso.with(VistaPerfilActivity.this)     .load(user.getPhotoUrl())     .resize(500,500)     .centerCrop()     .into(imvAvatar);        //HERE GOES THE document DATA      }else{      }    } else {      finish();    }  });}

如您所见,我们的

`回调与
OnCompleteListener`Firestore本身非常相似,它只是针对我们的需求量身定制了。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号