Firebase Android:数据库增长时出现问题
在测试期间,我的数据库中有大约30个引号,大约有6个用户 我现在添加了我的报价的其余部分 总数约为2000: 问题: 加载应用程序需要很长时间,它会崩溃并转到页面等待应用程序或退出。然后我减少到200个报价,但仍有问题(崩溃前可能会显示一个报价) 我在做什么? 我在数据库中引用我的报价,每次有人打开主页时,我都会得到一个随机报价(不知道如何每天得到一个报价) 引用的JSON片段:Firebase Android:数据库增长时出现问题,android,json,firebase,firebase-realtime-database,Android,Json,Firebase,Firebase Realtime Database,在测试期间,我的数据库中有大约30个引号,大约有6个用户 我现在添加了我的报价的其余部分 总数约为2000: 问题: 加载应用程序需要很长时间,它会崩溃并转到页面等待应用程序或退出。然后我减少到200个报价,但仍有问题(崩溃前可能会显示一个报价) 我在做什么? 我在数据库中引用我的报价,每次有人打开主页时,我都会得到一个随机报价(不知道如何每天得到一个报价) 引用的JSON片段: { "Quotes": { "1": { "Name": "Abbe
{
"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
}
}
}
。。。。。。。。等
我的代码:
{
"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数据库根目录中记录大小
您只需读取大小,并生成随机整数以获得报价,或先生成随机数公式,即可轻松指定报价,无需下载所有内容。这是否回答了您的问题?