【问题标题】:Firestore realtime update is not working on recyclerviewFirestore 实时更新不适用于 recyclerview
【发布时间】:2018-08-19 09:57:28
【问题描述】:

我正在使用 Cloud Firestore 来填充 recyclerview。

但是,实时更新不起作用,重新启动应用程序后会显示更改。我还在 recyclerview 上添加了分页。文档添加和修改仅在应用重启时显示。

加载我的帖子:

private fun loadMorePost() {
    val nextQuery = shopsCollection
            .orderBy("timestamp", Query.Direction.DESCENDING)
            .startAfter(lastVisible)
            .limit(3)

    nextQuery.addSnapshotListener({ documentSnapshots, e ->
        if (!documentSnapshots.isEmpty) {

            lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]
            for (doc in documentSnapshots) {

                    val shop = doc.toObject(Shop::class.java)
                    shopList.add(shop)

                    mAdapter.notifyDataSetChanged()

                }
            }
    })
}

onStart():

public override fun onStart() {
    super.onStart()
    val user = FirebaseAuth.getInstance().currentUser
    val isUserFirstTime = java.lang.Boolean.valueOf(Utility.readSharedSetting(applicationContext, Utility.PREF_USER_FIRST_TIME, "true"))
    val introIntent = Intent(this@MainActivity, OnBoardActivity::class.java)
    introIntent.putExtra(Utility.PREF_USER_FIRST_TIME, isUserFirstTime)
    if (isUserFirstTime) {
        startActivity(introIntent)
        finish()
    } else if (user == null) {
        startActivity(Intent(this@MainActivity, WelcomeActivity::class.java))
        finish()
    } else if (!isUserFirstTime) {
        val firstQuery = shopsCollection.orderBy("timestamp", Query.Direction.DESCENDING).limit(3)
        firstQuery.addSnapshotListener({ documentSnapshots, e ->
            lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]

            for (doc in documentSnapshots) {
                showFabButton()
                val blogPost = doc.toObject(Shop::class.java)
                shopList.add(blogPost)

                mAdapter.notifyDataSetChanged()
              }
          })
       }
    }
 }

【问题讨论】:

  • 有更新时是你的addSnapshotListener吗?例如。如果您在那里添加一些日志记录或设置断点,它是否会在有更新时记录/触发?
  • 但它不起作用,更改仅显示重新启动应用程序
  • @user8209569 这正是弗兰克想要弄清楚的。可能是没有调用 Listener,列表没有正确更改,或者更新后更改可能无法从列表正确反映到视图。
  • 请在RecyclerView 中发布您的适配器以及您设置适配器的方式。
  • P.S.您在添加每个项目后调用 notifyDatasetChanged 。将其移出 for 循环

标签: android firebase-realtime-database android-recyclerview kotlin google-cloud-firestore


【解决方案1】:

我假设您所指的数据库的更改是删除:

在您的代码中,您可以添加新项目,但不会删除可能的删除。因此,将您的代码更改为以下内容应该可以:

nextQuery.addSnapshotListener({ documentSnapshots, e ->
    if (!documentSnapshots.isEmpty) {
        shopList.clear()
        lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]
        for (doc in documentSnapshots) {
                val shop = doc.toObject(Shop::class.java)
                shopList.add(shop)
                mAdapter.notifyDataSetChanged()
            }
        }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 2019-07-02
    相关资源
    最近更新 更多