I have an Android app, Like any other android app I have to fill in data that is to be retrieved from Firebase Realtime Database, now over time, I have noticed queries grew to over 100s, Now every time I had to read data I have to add ValueEventListeners to references. To avoid using lengthy references I created a static Constants class that stores all references. Example
public class Constants {
public static final String THUMB_URL= "https://www.qualcomm.com/sites/ember/files/styles/scale_480/public/components/two-column-hdi/side/blue-ai-inception-badge-updated-2.png?itok=cOkbug7y";
public static final FirebaseUser CURR_USER= FirebaseAuth.getInstance().getCurrentUser();
public static final String UID= FirebaseAuth.getInstance().getCurrentUser().getUid();
public static final DatabaseReference ROOT_REF=FirebaseDatabase.getInstance().getReference();
public static final DatabaseReference QUIZ_REF=ROOT_REF.child("quizzes");
public static final DatabaseReference QUIZ_HOF_REF=QUIZ_REF.child("hof");
public static final DatabaseReference QUIZ_HOF_Q_REF=QUIZ_HOF_REF.child("questions");
public static final DatabaseReference QUIZ_HOF_TOTQ_REF=QUIZ_HOF_REF.child("totalQuestions");
public static final DatabaseReference SCORECARD_REF=ROOT_REF.child("scorecards");
public static final DatabaseReference SCORECARD_UID_REF=SCORECARD_REF.child(UID);
//Similar declarations
}
Now to make queries I created a Class with static functions, which make use of that references. Example
public final class MyUtilsApp {
public static void queryByQuestionNumber(String quizName, String questionNumber) {
Query query = Constants.QUIZ_REF.child(quizName).child("questions")
.orderByChild("questionNumber").equalTo(questionNumber).limitToFirst(1);
query.addChildEventListener(new ChildEventListener() {..});
}
public static void queryAllQuestions(String quizName){
Query query=Constants.QUIZ_REF.child(quizName).child("questions").orderByChild("questionNumber");
query.addChildEventListener(new ChildEventListener() {..});
}
public static void upgradeScore(String quizName){
DatabaseReference reference=Constants.SCORECARD_UID_REF.child("score");
reference.addListenerForSingleValueEvent(new ValueEventListener(){...});
}
//Similar Functions
}
Now back to the question, How wrong or right is this in managing queries in Android. Especially when queries grow over 50s?
question from:
https://stackoverflow.com/questions/65851858/how-to-manage-100s-of-firebase-queries 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…