在测试期间,我的数据库中有大约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
}
}
}
我要的是什么?
我希望既可以每天显示报价,也可以随机显示报价。如果我继续随机给出报价,我该怎么做以确保其不会使应用程序过载并崩溃。我希望有很多引号,而不是经常删除和添加新引号。
答案 0 :(得分:0)
1。“加载该应用需要很长时间”
您有databaseRef.keepSynced(true);
吗?
这将加载所有数据库,因此将花费很长时间
您应该只阅读想要的内容
或者您可以FirebaseDatabase.getInstance().setPersistenceEnabled(Base.dataPersistence);
它将保留数据而无需长时间重复读取
2。您应该检查您的数据库,如果格式错误,它将失效
3。我有最简单的方法,您只需在firebase datebase根目录中记录大小
您只需阅读大小,然后生成随机整数以获取报价,或者先创建一个随机数公式即可轻松指定报价,而无需全部下载。