0

During testing I had about 30 quotes in my data base and about 6 users.

i now added rest of my quote.

Total is about 2000:

Problem: It takes so long for the app to load, it crashes and goes to the page wait for the app or exit. Then I cut down to 200 quotes, but having issue still (might show one quote before crashing)

What i am doing? I am referencing my quotes in my database and getting a random quote every time someone open the main page (Did not know how to get one quote a day)

Snippet of JSON for the quotes:

    {
   "Quotes": {
       "1": {
          "Name": "Abbey, Edward",
          "Quote": "In social institutions, the whole is always less than the sum of its parts. There will never be a state as good as its people, or a church worthy of its congregation, or a university equal to its faculty and students."
       },
       "2": {
          "Name": "Adams, George Matthew",
          "Quote": "There is no such thing as a self-made man. We are made up of thousands of others. Everyone who has ever done a kind deed for us, or spoken one word of encouragement to us, has entered into the makeup of our character and our thoughts, as well as our success."
       },
       "3": {
          "Name": "Albani, Emma",
          "Quote": "I had always loved beautiful and artistic things, though before leav"
       },
       "4": {
          "Name": "Borman, Frank",
          "Quote": "Exploration is really the essence of the human spirit."
       },

........ etc

my code:

private void showQuote(){

    databaseReference = FirebaseDatabase.getInstance().getReference("Quotes");
    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            int count = (int) dataSnapshot.getChildrenCount();
            for(DataSnapshot data: dataSnapshot.getChildren()){
                int rand = new Random().nextInt(count);
                for (int i = 0; i < rand; i++) {
                    String authorName = data.child("Name").getValue().toString();
                    String quoteGiven = data.child("Quote").getValue().toString();
                    name.setText("- " + authorName);
                    quote.setText(quoteGiven);
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(MainActivity.this, "Loading Quote failed", Toast.LENGTH_SHORT).show();
        }
    });

}

My Rules:

 {
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth != null && auth.uid == $uid",
        ".write": "auth != null && auth.uid == $uid"
       }
      },
      "Quotes": {
       ".read": true,
        ".write": false
    }
  }
}

What i am asking for?

I want to be able either show a quote everyday, and if that not possible to randomly show a quote. If i continue to do the randomly give a quote, what can i do to make sure its not overloading the app and crashing it. I want to have a lot of quotes rather than delete and add new ones every so often.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
AHPV
  • 77
  • 7
  • Does this answer your question? [Select random entry from Firebase Realtime Database in Java](https://stackoverflow.com/questions/50399597/select-random-entry-from-firebase-realtime-database-in-java) – Christilyn Arjona Dec 13 '19 at 06:04

1 Answers1

0

1."It takes so long for the app to load"

do you have databaseRef.keepSynced(true);?

this will load all database, so will takes very long

You should only read what you want

or you can FirebaseDatabase.getInstance().setPersistenceEnabled(Base.dataPersistence);

it will retain data without repeating read long time

2.You should check your database,If the format is wrong then it will die

3.I have the easiest way, you just record size at your firebase datebase Root directory,

you just read size,and produce random int to get quote,Or make a random number formula first,can easily specify quote,no need to download all.

Ruyut
  • 151
  • 11