Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

java - How to manage 100s of firebase queries

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no problem if you use this, but I recommend using AsyncTask() to ensure that all queries are not being queried at the same time !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...