- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在构建一个大量使用 Firebase 数据库的 Android 应用。
在数据库中,我有一个名为 RootNode 的节点,它包含 100 个子节点。 RootNode 的每个子节点都有自己的子节点(每个子节点有 30 到 60 个),每个子节点都有自己的键值属性(因此,我们正在谈论每个子节点的大量数据)。
客户端,我将 RootNode 的所有子节点的 key 存储在 List<String> list
中.通过遍历此列表,我向 RootNode 的每个子节点添加了一个 ValueEventListener。
// Loop through the children of RootNode.
for (final String keyOfChildNode: list) { // list contains the keys of all the children nodes of RootNode.
Log.d(TAG, "Adding ValueEventListener to child node: " + keyOfChildNode);
// Set a ValueEventListener to child node.
RootNodeReference.child(keyOfChildNode).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Datasnapshot of child has been retrieved.
Log.d(TAG, "Datasnapshot retrieved for child node: " + keyOfChildNode);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Report error.
}
});
}
这是控制台输出:
Adding ValueEventListener to child node: keyChildNode001
Adding ValueEventListener to child node: keyChildNode002
Adding ValueEventListener to child node: keyChildNode003
Adding ValueEventListener to child node: keyChildNode004
Adding ValueEventListener to child node: keyChildNode005
Adding ValueEventListener to child node: keyChildNode006
Adding ValueEventListener to child node: keyChildNode007
Adding ValueEventListener to child node: keyChildNode008
Adding ValueEventListener to child node: keyChildNode009
Adding ValueEventListener to child node: keyChildNode010
Adding ValueEventListener to child node: keyChildNode011
Adding ValueEventListener to child node: keyChildNode012
Adding ValueEventListener to child node: keyChildNode013
Adding ValueEventListener to child node: keyChildNode014
Adding ValueEventListener to child node: keyChildNode015
Adding ValueEventListener to child node: keyChildNode016
Adding ValueEventListener to child node: keyChildNode017
Adding ValueEventListener to child node: keyChildNode018
Adding ValueEventListener to child node: keyChildNode019
Adding ValueEventListener to child node: keyChildNode020
Adding ValueEventListener to child node: keyChildNode021
Adding ValueEventListener to child node: keyChildNode022
Adding ValueEventListener to child node: keyChildNode023
Adding ValueEventListener to child node: keyChildNode024
Adding ValueEventListener to child node: keyChildNode025
Adding ValueEventListener to child node: keyChildNode026
Adding ValueEventListener to child node: keyChildNode027
Adding ValueEventListener to child node: keyChildNode028
Adding ValueEventListener to child node: keyChildNode029
Adding ValueEventListener to child node: keyChildNode030
Adding ValueEventListener to child node: keyChildNode031
Adding ValueEventListener to child node: keyChildNode032
Adding ValueEventListener to child node: keyChildNode033
Adding ValueEventListener to child node: keyChildNode034
Adding ValueEventListener to child node: keyChildNode035
Adding ValueEventListener to child node: keyChildNode036
Adding ValueEventListener to child node: keyChildNode037
Adding ValueEventListener to child node: keyChildNode038
Adding ValueEventListener to child node: keyChildNode039
Adding ValueEventListener to child node: keyChildNode040
Adding ValueEventListener to child node: keyChildNode041
Adding ValueEventListener to child node: keyChildNode042
Adding ValueEventListener to child node: keyChildNode043
Adding ValueEventListener to child node: keyChildNode044
Adding ValueEventListener to child node: keyChildNode045
Adding ValueEventListener to child node: keyChildNode046
Adding ValueEventListener to child node: keyChildNode047
Adding ValueEventListener to child node: keyChildNode048
Adding ValueEventListener to child node: keyChildNode049
Adding ValueEventListener to child node: keyChildNode050
Adding ValueEventListener to child node: keyChildNode051
Adding ValueEventListener to child node: keyChildNode052
Adding ValueEventListener to child node: keyChildNode053
Adding ValueEventListener to child node: keyChildNode054
Adding ValueEventListener to child node: keyChildNode055
Adding ValueEventListener to child node: keyChildNode056
Adding ValueEventListener to child node: keyChildNode057
Adding ValueEventListener to child node: keyChildNode058
Adding ValueEventListener to child node: keyChildNode059
Adding ValueEventListener to child node: keyChildNode060
Adding ValueEventListener to child node: keyChildNode061
Adding ValueEventListener to child node: keyChildNode062
Adding ValueEventListener to child node: keyChildNode063
Adding ValueEventListener to child node: keyChildNode064
Adding ValueEventListener to child node: keyChildNode065
Adding ValueEventListener to child node: keyChildNode066
Adding ValueEventListener to child node: keyChildNode067
Adding ValueEventListener to child node: keyChildNode068
Adding ValueEventListener to child node: keyChildNode069
Adding ValueEventListener to child node: keyChildNode070
Adding ValueEventListener to child node: keyChildNode071
Adding ValueEventListener to child node: keyChildNode072
Adding ValueEventListener to child node: keyChildNode073
Adding ValueEventListener to child node: keyChildNode074
Adding ValueEventListener to child node: keyChildNode075
Adding ValueEventListener to child node: keyChildNode076
Adding ValueEventListener to child node: keyChildNode077
Adding ValueEventListener to child node: keyChildNode078
Adding ValueEventListener to child node: keyChildNode079
Adding ValueEventListener to child node: keyChildNode080
Adding ValueEventListener to child node: keyChildNode081
Adding ValueEventListener to child node: keyChildNode082
Adding ValueEventListener to child node: keyChildNode083
Adding ValueEventListener to child node: keyChildNode084
Adding ValueEventListener to child node: keyChildNode085
Adding ValueEventListener to child node: keyChildNode086
Adding ValueEventListener to child node: keyChildNode087
Adding ValueEventListener to child node: keyChildNode088
Adding ValueEventListener to child node: keyChildNode089
Adding ValueEventListener to child node: keyChildNode090
Adding ValueEventListener to child node: keyChildNode091
Adding ValueEventListener to child node: keyChildNode092
Adding ValueEventListener to child node: keyChildNode093
Adding ValueEventListener to child node: keyChildNode094
Adding ValueEventListener to child node: keyChildNode095
Adding ValueEventListener to child node: keyChildNode096
Adding ValueEventListener to child node: keyChildNode097
Adding ValueEventListener to child node: keyChildNode098
Adding ValueEventListener to child node: keyChildNode099
Adding ValueEventListener to child node: keyChildNode100
Datasnapshot retrieved for child node: keyChildNode001
Datasnapshot retrieved for child node: keyChildNode002
Datasnapshot retrieved for child node: keyChildNode003
Datasnapshot retrieved for child node: keyChildNode004
Datasnapshot retrieved for child node: keyChildNode005
Datasnapshot retrieved for child node: keyChildNode006
Datasnapshot retrieved for child node: keyChildNode007
Datasnapshot retrieved for child node: keyChildNode008
Datasnapshot retrieved for child node: keyChildNode009
Datasnapshot retrieved for child node: keyChildNode010
Datasnapshot retrieved for child node: keyChildNode011
Datasnapshot retrieved for child node: keyChildNode012
Datasnapshot retrieved for child node: keyChildNode013
Datasnapshot retrieved for child node: keyChildNode014
Datasnapshot retrieved for child node: keyChildNode015
Datasnapshot retrieved for child node: keyChildNode016
Datasnapshot retrieved for child node: keyChildNode017
Datasnapshot retrieved for child node: keyChildNode018
Datasnapshot retrieved for child node: keyChildNode019
Datasnapshot retrieved for child node: keyChildNode020
Datasnapshot retrieved for child node: keyChildNode021
Datasnapshot retrieved for child node: keyChildNode022
Datasnapshot retrieved for child node: keyChildNode023
Datasnapshot retrieved for child node: keyChildNode024
Datasnapshot retrieved for child node: keyChildNode025
Datasnapshot retrieved for child node: keyChildNode026
Datasnapshot retrieved for child node: keyChildNode027
Datasnapshot retrieved for child node: keyChildNode028
Datasnapshot retrieved for child node: keyChildNode029
Datasnapshot retrieved for child node: keyChildNode030
Datasnapshot retrieved for child node: keyChildNode031
Datasnapshot retrieved for child node: keyChildNode032
Datasnapshot retrieved for child node: keyChildNode033
Datasnapshot retrieved for child node: keyChildNode034
Datasnapshot retrieved for child node: keyChildNode035
Datasnapshot retrieved for child node: keyChildNode036
Datasnapshot retrieved for child node: keyChildNode037
Datasnapshot retrieved for child node: keyChildNode038
Datasnapshot retrieved for child node: keyChildNode039
Datasnapshot retrieved for child node: keyChildNode040
Datasnapshot retrieved for child node: keyChildNode041
Datasnapshot retrieved for child node: keyChildNode042
Datasnapshot retrieved for child node: keyChildNode043
Datasnapshot retrieved for child node: keyChildNode044
Datasnapshot retrieved for child node: keyChildNode045
Datasnapshot retrieved for child node: keyChildNode046
Datasnapshot retrieved for child node: keyChildNode047
Datasnapshot retrieved for child node: keyChildNode048
Datasnapshot retrieved for child node: keyChildNode049
Datasnapshot retrieved for child node: keyChildNode050
Datasnapshot retrieved for child node: keyChildNode051
Datasnapshot retrieved for child node: keyChildNode052
Datasnapshot retrieved for child node: keyChildNode053
Datasnapshot retrieved for child node: keyChildNode054
Datasnapshot retrieved for child node: keyChildNode055
Datasnapshot retrieved for child node: keyChildNode056
Datasnapshot retrieved for child node: keyChildNode057
Datasnapshot retrieved for child node: keyChildNode058
Datasnapshot retrieved for child node: keyChildNode059
Datasnapshot retrieved for child node: keyChildNode060
Datasnapshot retrieved for child node: keyChildNode061
Datasnapshot retrieved for child node: keyChildNode062
Datasnapshot retrieved for child node: keyChildNode063
Datasnapshot retrieved for child node: keyChildNode064
Datasnapshot retrieved for child node: keyChildNode065
Datasnapshot retrieved for child node: keyChildNode066
Datasnapshot retrieved for child node: keyChildNode067
Datasnapshot retrieved for child node: keyChildNode068
Datasnapshot retrieved for child node: keyChildNode069
Datasnapshot retrieved for child node: keyChildNode070
Datasnapshot retrieved for child node: keyChildNode071
Datasnapshot retrieved for child node: keyChildNode072
Datasnapshot retrieved for child node: keyChildNode073
Datasnapshot retrieved for child node: keyChildNode074
Datasnapshot retrieved for child node: keyChildNode075
Datasnapshot retrieved for child node: keyChildNode076
Datasnapshot retrieved for child node: keyChildNode077
Datasnapshot retrieved for child node: keyChildNode078
Datasnapshot retrieved for child node: keyChildNode079
Datasnapshot retrieved for child node: keyChildNode080
Datasnapshot retrieved for child node: keyChildNode081
Datasnapshot retrieved for child node: keyChildNode082
Datasnapshot retrieved for child node: keyChildNode083
Datasnapshot retrieved for child node: keyChildNode084
Datasnapshot retrieved for child node: keyChildNode085
Datasnapshot retrieved for child node: keyChildNode086
Datasnapshot retrieved for child node: keyChildNode087
Datasnapshot retrieved for child node: keyChildNode088
Datasnapshot retrieved for child node: keyChildNode089
Datasnapshot retrieved for child node: keyChildNode090
Datasnapshot retrieved for child node: keyChildNode091
Datasnapshot retrieved for child node: keyChildNode092
Datasnapshot retrieved for child node: keyChildNode093
Datasnapshot retrieved for child node: keyChildNode094
Datasnapshot retrieved for child node: keyChildNode095
Datasnapshot retrieved for child node: keyChildNode096
Datasnapshot retrieved for child node: keyChildNode097
Datasnapshot retrieved for child node: keyChildNode098
Datasnapshot retrieved for child node: keyChildNode099
Datasnapshot retrieved for child node: keyChildNode100
有一件事让我感到惊讶:由于添加到子引用的 ValueEventListeners 本质上是异步的,我预计快照将以随机顺序检索(不一定与 ValueEventListeners 的顺序相同)添加)。但控制台输出显示快照是按照将 ValueEventListeners 添加到每个子节点的顺序检索的。
以这种“有序”的方式检索快照是否正常?如果是,考虑到监听器的异步性质,这怎么可能?提前谢谢你。
最佳答案
这确实是预期的行为。
Firebase 数据库客户端通过单个套接字连接与其服务器进行通信。此套接字用于客户端和数据库服务器之间的所有通信。
当您请求 100 条记录时,所有请求都按照您进行调用的顺序通过这个单一连接发送。然后服务器以相同的顺序执行请求并以相同的顺序返回结果。
这称为流水线:通过单个连接发送多个请求并按顺序返回结果。我之前在这里回答了更多:Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly
关于android - Firebase Database Android - 来自多个 ValueEventListeners 的快照检索顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41572610/
我有一个 Firebase valueEvent 监听器: questions.orderByChild("CategoryID").equalTo(categoryID)
我正在使用 ValueEventListeners 从 Firebase 数据库中检索数据。现在我想从查询中删除 ValueEventListener。像这样: query.removeEventLi
出于某种原因,当我在 fragment 中调用 getTitle() 时,它总是返回 null。似乎根本没有添加 ValueEventListener。 当我在监听器内调试或打印 title 的值时,
单击按钮后,我需要从 Firebase 实时数据库获取昵称值。所以我在按钮的 onClick 方法中创建了 singleValueListner() 。但这不起作用。 我尝试过调试它,但代码没有进入s
值事件监听器在每个 Activity 中都正常工作。但在我的注册 Activity 中,花费了超过 2 分钟。我不知道为什么。 这是我的方法 private void verifyuserinpu
我有一个 valueEventListener() 用于从 Firebase 获取数据。 这是第一个。 sectionReference.child(classID).addValueEventLis
我有这个标准的 Firebase 调用: ref.child("user/toys/-K2873267832") .addListenerForSingleValueEvent(new Valu
应用说明 我的应用程序从 Firebase 数据库中检索数据。首先,我将 java 对象正确推送到数据库,并且可以毫无问题地检索数据,使用 Fragments onActivityCreated 中的
我有一个名为“bg”的全局变量。它在 firebase 数据库引用“myRef”的 ValueEventListener 中初始化 但是如果我尝试在 valueeventlistener block
DatabaseReference Ref = FirebaseDatabase.getInstance().getReference(Constants.Client + "/" + path);
我正在尝试使用信号量来等待我的 firebase valueEventListener。我有一个用户信息 Activity ,其中包含用户必须填写的 6 个不同字段。当用户保存他/她的信息时,我想进行
我正在尝试从 Firebase 获取字符串列表,只有在检索到所有数据后,我才会更新我的 UI。目前我在 fragment 类中使用以下代码来实现此目的。 taskList = new ArrayLis
在我下面的代码中,函数 setPhoto(url) 始终接收空 url,但如果我通过单击按钮调用另一个函数并且它需要该 url,它就会正确接收数据。 @Override protected void
我正在尝试使用代码 straight from their documentation 从 Firebase 数据库中读取数据。我的代码如下,当我尝试将 ValueEventListener 添加到我
我有一个使用 Google Firebase 的 Android 应用。 我需要下载带有随机 ID 的图像。 我有一个函数必须返回随机图片的位图。在此函数中,我从单独的数据库条目中读取图片数量,然后生
我正在尝试检测 Firebase 数据库中的值更改。这是我初始化 ValueEventListener 的代码: valueEventListener = new ValueEventListener
我的 Android 应用中遇到了 Firebase 实时数据库的问题。 我正在尝试的是实时更新列表。但更新仅适用于应用程序启动。它不会自动更新。因此,我需要在需要更新时重新启动应用程序。 有没有遗漏
我正在尝试使用接口(interface)异步检查我的 Firebase 数据库中的条目,但我不确定如何以与密码和电子邮件相同的方式实现它。 例子: if (TextUtils.isEmpty(pass
我已经追踪我的代码找到崩溃的根本原因,显然这个构造函数无法更新类的变量。当我试图从此类获取数据时出现空指针异常。您可以安全地假设记录已经在数据库中,它所要做的只是获取数据并将其放入类/对象中。我现在只
我试图返回一个 bool 值,但返回的值始终为 false,即在本例中为 0。 检查变量是一个实例变量,按下按钮时将调用以下函数。 private boolean checkAnswerSubmiss
我是一名优秀的程序员,十分优秀!