Firebase Android(数据库增长时出现问题)

登山则情满于山,观海则意溢于海。这篇文章主要讲述Firebase Android:数据库增长时出现问题相关的知识,希望能为你提供帮助。
在测试期间,我的数据库中有大约30个引号,大约有6个用户。
我现在添加了其余的报价。
总计约2000:
问题:应用程序加载需要花费很长时间,它会崩溃并转到页面等待应用程序或退出。然后我减少到200个引号,但是仍然出现问题(可能在崩溃前显示一个引号)
我在做什么?我在数据库中引用我的报价,每次有人打开主页时都会得到一个随机报价(不知道每天如何获得一个报价)
JSON的引号片段:

{ "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." },

........等
我的代码:
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(); } }); }

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

我要什么?
我希望既可以每天显示报价,也可以随机显示报价。如果我继续随机给出报价,我该怎么做以确保其不会使应用程序过载并崩溃。我希望有很多引号,而不是经常删除和添加新引号。
答案1。“应用程序加载需要很长时间”
您有databaseRef.keepSynced(true); 吗?
这将加载所有数据库,因此将花费很长时间
您应该只阅读想要的内容
或者您可以FirebaseDatabase.getInstance().setPersistenceEnabled(Base.dataPersistence);
它将保留数据而无需长时间重复读取
2。您应该检查您的数据库,如果格式错误,它将消失
3。我有最简单的方法,您只需在firebase datebase根目录中记录大小,
【Firebase Android(数据库增长时出现问题)】您只需读取大小,并产生一个随机的int来获取报价,或者先制作一个随机数公式,就可以轻松地指定报价,而无需全部下载。

    推荐阅读