将值分配给内部类AsyncTask Firebase中的最后一个元素数组变量

时间:2018-04-03 20:22:46

标签: java android

这里我试图从内部类[FirebaseFireStore类]为最终的一个元素数组赋值,但该值永远不会改变。

final boolean[] canPost = {false};
                mFirestore.collection("Users").document(current_user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            if (task.getResult().exists()) {

                                try {
                                    String lastPost = task.getResult().get("last_posted").toString();
                                    boolean isAbleToPost = canHePost(lastPost);
                                    if (!isAbleToPost){
                                        Log.d(TAG, "Can't post");
                                        canPost[0] = false;
                                    }else{
                                        Log.d(TAG, "CAN POST!!");
                                        canPost[0] = true;
                                    }
                                } catch (NullPointerException e) {
                                    Log.d(TAG, "User never posted before");
                                }
                            }
                        }
                    }
                });

                Log.d("isAllowed to post?", ""+ canPost[0]);

请注意,boolean isAbleToPost = canHePost(lastPost);返回ture 并且Log.d(TAG, "CAN POST!!");显示在日志中,但让我感到困惑的是,最后一个Log.d("isAllowed to post?", ""+ canPost[0]);显示在第一个日志之前! 记录顺序:

D/isAllowed to post?: false
D/CAN: CAN POST!!

1 个答案:

答案 0 :(得分:1)

你获取异步数据,所以当然如果你内联你的代码你创建一个布尔数组然后你的提取数据并添加一个会触发以后的监听器,最后你会记录Log.d("isAllowed to post?", ""+ canPost[0]);

如果您希望登录回调,请将其移至

final boolean[] canPost = {false};
                mFirestore.collection("Users").document(current_user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            if (task.getResult().exists()) {

                                try {
                                    String lastPost = task.getResult().get("last_posted").toString();
                                    boolean isAbleToPost = canHePost(lastPost);
                                    if (!isAbleToPost){
                                        Log.d(TAG, "Can't post");
                                        canPost[0] = false;
                                    }else{
                                        Log.d(TAG, "CAN POST!!");
                                        canPost[0] = true;
                                    }
                                } catch (NullPointerException e) {
                                    Log.d(TAG, "User never posted before");
                                }
                            }
                        }
                        Log.d("isAllowed to post?", ""+ canPost[0]);
                     }
                });