问题是关于此代码(在 Android 应用程序中):
private Task<QuerySnapshot> getVotesFromDB() {
    res = new int[]{0, 0, 0};   // a private class-member
    return FirebaseFirestore.getInstance().collection("Votes")
            .whereEqualTo("proposition_key", curr_proposition.getKey())
            .get().addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        String userChoice = (String) document.get("user_choice");
                        int choice;
                        switch (userChoice) {
                            case "against":
                                choice = 0;
                                break;
                            case "impossible":
                                choice = 1;
                                break;
                            case "agreement":
                                choice = 2;
                                break;
                            default:
                                throw new IllegalStateException("Unexpected value: "   userChoice);
                        }
                        res[choice]  ;
                    }
                }
            });
}
通常,代码从 Firestore 集合中读取一些行,并对它们应用一些“业务逻辑”(计算每种型别的字符串数)。将来,业务逻辑可能会变得比计数复杂得多。所以我正在寻找一种重构代码的方法,这样业务逻辑就可以与数据库分开撰写和测验。我想要的是具有以下形式的功能:
int[] countVotes(Generator<String> strings) {
     res = new int[3];
     (for String userChoice: strings)  {
          // Update res as above
     }
     return res;
}
无需任何数据库连接即可进行单元测验。那么,上面的函式可以重构如下:
private Generator<String> getVotesFromDB() {
    return FirebaseFirestore.getInstance().collection("Votes")
            .whereEqualTo("proposition_key", curr_proposition.getKey())
            .get().addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        userChoice = (String) document.get("user_choice");
                        yield userChoice;
                    }
                }
            });
}
并运行类似的东西:
countVotes(getVotesFromDB())
问题是,我不知道如何使用异步函式呼叫来做到这一点。有没有办法以类似或更好的方式重构代码?
uj5u.com热心网友回复:
您可以将结果收集到一个阵列中,然后使用单独的方法对其进行处理。然后,如果处理方法很复杂,则可以针对各种不同的结果串列单独进行单元测验。
private void getVotesFromDB() {
    FirebaseFirestore.getInstance().collection("Votes")
        .whereEqualTo("proposition_key", curr_proposition.getKey())
        .get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                ArrayList<String> results = new ArrayList();
                for (QueryDocumentSnapshot document : task.getResult()) {
                    String userChoice = (String) document.get("user_choice");
                    results.add(userChoice);
                }
                // assign the result to a class member
                res = countVotes(results);
                // do something to signal to the rest of the code
                // that results have been processed (e.g. post to
                // LiveData or call another "showResults" method)
            }
        });
}
然后,任何复杂的计数逻辑都可以与 firebase 呼叫分开。
int[] countVotes(ArrayList<String> choices) {
    int[] res = new int[]{0,0,0}; 
    // count the votes
    return res;
}

 
							 
										
										 
										
										 
										
										
										 
										
										 
										
										 
										
										
0 评论