【发布时间】:2022-01-09 21:49:48
【问题描述】:
我正在尝试从 firebase 获取最近 30 天的帖子,并且我正在尝试使用帖子中的时间戳来做到这一点。但我无法获取数据。
Kotlin 代码:
var database:DatabaseReference=Firebase.database.reference
database.orderByChild("timestamp")
val postListener = object : ValueEventListener {
@RequiresApi(Build.VERSION_CODES.O)
override fun onDataChange(dataSnapshot: DataSnapshot) {
// Get Post object and use the values to update the UI
for(postSnapshot in dataSnapshot.children)
{
if(postSnapshot.child("timestamp").value.toString().toInt()>Time.from(Instant.now().minusSeconds(30*24*60*60)).toString().toInt())
{
val Post = postSnapshot.getValue(PostsModel::class.java)
postsList.add(Post!!)
}
}
}
override fun onCancelled(databaseError: DatabaseError) {
// Getting Post failed, log a message
Timber.d("failed")
}
}
【问题讨论】:
-
“我无法获取数据”真的很难解决,而且 Stack Overflow 是一个出了名的低效交互式调试器。如果您在调试器中运行代码,它会到达
onDataChange吗?如果是这样,它会进入for循环吗?如果是这样,它从timestamp读取的值是您所期望的吗?如果是,30 天前的值是您期望的值吗? -
不,它不会进入 onDataChange
-
请多一些自我探索,因为 Stack Overflow 是一个出了名的低效交互式调试器。那么它会到达
onCancelled吗?如果这也没有发生,当您附加侦听器时,是否有任何内容写入您的应用程序的 logcat?
标签: android firebase kotlin firebase-realtime-database