1 回答
TA贡献1853条经验 获得超6个赞
正如 Selvin 评论的那样:数据是从 Firebase 异步加载的。您无法可靠地等待数据可用。请参阅在 Firebase Listener 中设置 Singleton 属性值。
该解决方案是移动从火力地堡数据需要的代码到所述onDataChange中checkDataNew:
fun checkDataNew() {
var rootRef=FirebaseDatabase.getInstance().getReference("BG Data")
// Read from the database
rootRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
var isKeyFound = false; // local variables
var foundKey;
// This method is called once with the initial value and again
// whenever data at this location is updated.
for(data:DataSnapshot in dataSnapshot.children)
{
var oldEvent=data.child("recentEvent").getValue().toString()
var oldDate:String=data.child("calendarTime").getValue().toString()
var oldEmailID:String=data.child("emailID").getValue().toString()
if(oldEvent.equals(recentEvent) && oldDate.equals(calendarTime) && oldEmailID.equals(emailID)) {
foundKey = data.key.toString()
isKeyFound = true
}
}
// TODO: process the result here
if (isKeyFound) {
...
} else {
...
}
}
override fun onCancelled(error: DatabaseError) {
// Failed to read value
}
})
}
或者,您可以定义自己的回调接口,将其传入checkDataNew并从那里调用它。有关此示例,请参阅getContactsFromFirebase() 方法返回空列表。
添加回答
举报