【问题标题】:Having trouble with retrived data from firebase while comparing using if在比较使用 if 时从 firebase 检索数据时遇到问题
【发布时间】:2022-01-19 18:18:56
【问题描述】:

我是 android studio 的新手,真的可以使用您的帮助。 我在使用firebase auth登录后遇到登录页面代码问题。我会从登录页面的firestore中检索一个int值,然后决定下一步去哪里(布局)。它总是转到主要活动而不是个人详细信息2(即,即使 fristtimekey = 1 中的值,if 条件也始终为假)。

请检查问题出在 if(fristtimekey ==1),除此之外一切都正确(logcat 也正确,其中值为 1)。

这是我的代码:

fAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            Toast.makeText(Login.this, "LogedIn succesfully", Toast.LENGTH_SHORT).show();

            userid = fAuth.getCurrentUser().getUid();
            DocumentReference docRef = foster.collection("users").document(userid);
            DocumentReference document = foster.document("users/email");
            docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();
                        if (document.exists()) {
                            fristtimekey = document.getLong("Frist time");
                            Log.d("TAG", String.valueOf(fristtimekey));
                            Log.d("TAG", "DocumentSnapshot data: " + document.getLong("Frist time") + " int: " + fristtimekey);
                        } else {
                            Log.d("TAG", "No such document");
                        }
                    } else {
                        Log.d("TAG", "get failed with ", task.getException());
                    }
                }
            });
            progressBar.setVisibility(View.GONE);
            if (fristtimekey == 1) {
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
            } else {
                startActivity(new Intent(getApplicationContext(), PersonalDetails2.class));
            }
        } else {
            Toast.makeText(Login.this, "LogedIn unsuccesfully", Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.GONE);
        }
    }
}); 

【问题讨论】:

    标签: java android firebase if-statement google-cloud-firestore


    【解决方案1】:

    数据是从 Firestore(和大多数现代云 API)异步加载的,而您的主要代码会在此期间继续执行。然后当数据可用时,您的onComplete 会被调用,以便您可以使用该数据。

    在您当前的代码中,这意味着您的 if (fristtimekey == 1) { [原文如此] 在 fristtimekey = document.getLong("Frist time") 运行之前运行。如果您在这些行上设置断点并在调试器中运行代码,或者在这些行周围添加一些日志记录,您最容易看到这一点。

    这在实践中意味着任何需要来自数据库的数据的代码,必须在onComplete 中,从那里调用,或者以其他方式同步。

    将以上内容应用于您的代码意味着:

    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            progressBar.setVisibility(View.GONE); // ?
    
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    fristtimekey = document.getLong("Frist time");
                    Log.d("TAG", String.valueOf(fristtimekey));
                    Log.d("TAG", "DocumentSnapshot data: " + document.getLong("Frist time") + " int: " + fristtimekey);
                } else {
                    Log.d("TAG", "No such document");
                }
                // ?
                if (fristtimekey == 1) {
                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                } else {
                    startActivity(new Intent(getApplicationContext(), PersonalDetails2.class));
                }
            } else {
                Log.d("TAG", "get failed with ", task.getException());
            }
        }
    });
    

    我建议也检查一下:

    【讨论】:

      猜你喜欢
      • 2019-12-24
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      相关资源
      最近更新 更多