我想允许我的用户搜索其他用户并将其添加为朋友。 我的Firebase数据库中有大约6,000个用户节点,因此显然搜索它是一项非常繁重的操作。 我在“用户”节点创建了一个单独的节点,在根目录中名为“friendsIndex”,并使用搜索所需的最少信息填充它:
friendsIndex: {
$userID: {
name: "firstName",
lastName: "lastName",
profileImg: "urltoImage..."
},
....
}
我的数据库规则包含:
"friendsIndex":{
"$userID":{
".indexOn": ["name", "lastName"]
}
},
用户搜索功能如下:
Query mQuery = Database.child("friendsIndex").orderByChild("name").startAt(searchWord).endAt(searchWord+"\uf8ff");
mQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user;
for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
user = eventSnapshot.getValue(User.class);
//more code..
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
但我仍然收到这个错误:
08-31 13:03:28.354 3916-3925/com.learnArabic.anaAref E/System: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
08-31 13:03:28.364 3916-17998/com.learnArabic.anaAref E/RunLoop: Firebase Database encountered an OutOfMemoryError. You may need to reduce the amount of data you are syncing to the client (e.g. by using queries or syncing a deeper path). See https://firebase.google.com/docs/database/ios/structure-data#best_practices_for_data_structure and https://firebase.google.com/docs/database/android/retrieve-data#filtering_data
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
08-31 13:03:28.379 3916-3916/com.learnArabic.anaAref E/UncaughtException: java.lang.RuntimeException: Firebase Database encountered an OutOfMemoryError. You may need to reduce the amount of data you are syncing to the client (e.g. by using queries or syncing a deeper path). See https://firebase.google.com/docs/database/ios/structure-data#best_practices_for_data_structure and https://firebase.google.com/docs/database/android/retrieve-data#filtering_data
at com.google.android.gms.internal.mz.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
想不出任何不那么沉重的方式来执行此搜索,任何想法?
答案 0 :(得分:2)
例外是给出提示但是错误的网址。正确的过滤网址为https://firebase.google.com/docs/database/android/lists-of-data#filtering_data。在此页面中,方法limitToFirst()
可用于限制从列表开始的结果数。我认为这回答了你的问题:
无法想到执行此搜索的任何不太重要的方法,任何想法?
在两个查询之间删除用户时,您可以使用其他方法来处理这种情况。例如,您可以orderByKey()
和第二个查询继续使用startAt(lastKey)
。