android - 从 Firebase 检索数据时协程流错误

标签 android firebase google-cloud-firestore kotlin-coroutines

这是我第一次尝试使用 MVVM 架构模式将 Flow 与 Firebase 结合使用。 这里的问题是当我按下加载按钮时,数据加载到 TextView 中。但是当我上传新数据并尝试获取新数据和旧数据时它不起作用,它只显示旧数据。 重新启动应用程序后,它会提供新数据。 下面是代码。

存储库

@ExperimentalCoroutinesApi
class PostsRepository {

    private val mPostsCollection =
        FirebaseFirestore.getInstance().collection(Constants.COLLECTION_POST)

    fun getAllPosts() = flow<State<List<Post>>> {

        emit(State.loading())

        val snapshot = mPostsCollection.get().await()
        val posts = snapshot.toObjects((Post::class.java))

        emit(State.success(posts))

    }.catch {
        emit(State.failed(it.message.toString()))
    }.flowOn(Dispatchers.IO)


    fun addPosts(post: Post) = flow<State<DocumentReference>>
    {
        emit(State.loading())

        val postref = mPostsCollection.add(post).await()

        emit(State.success(postref))

    }.catch {
        emit(State.failed(it.message.toString()))
    }.flowOn(Dispatchers.IO)


}

状态类

sealed class State<T> {
    class Loading<T> : State<T>()
    data class Success<T>(val data: T) : State<T>()
    data class Failed<T>(val message: String) : State<T>()

    companion object {
        fun <T> loading() = Loading<T>()
        fun <T> success(data: T) = Success(data)
        fun <T> failed(message: String) = Failed<T>(message)

    }

}

View 模型

@ExperimentalCoroutinesApi
class MainViewModel(private val repository: PostsRepository):ViewModel() {


   // fun getAllposts() = repository.getAllPosts()

    val getallpostlivedata :LiveData<State<List<Post>>> = repository.getAllPosts().asLiveData()


    fun addpost(post: Post) = repository.addPosts(post)

}

主要 Activity


@ExperimentalCoroutinesApi
class MainActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var viewModel: MainViewModel

    private lateinit var binding: ActivityMainBinding


    private val uiScope = CoroutineScope(Dispatchers.Main)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        viewModel = ViewModelProvider(this, MainViewModelFactory()).get(MainViewModel::class.java)

        binding.buttonLoad.setOnClickListener(this)

        binding.buttonAdd.setOnClickListener(this)

    }




    private suspend fun addPost(post: Post) {

        viewModel.addpost(post).collect{ state ->
            when (state) {
                is State.Loading -> {
                    showToast("Loading")
                    binding.buttonAdd.isEnabled = false
                }

                is State.Success -> {
                    showToast("Posted")
                    binding.fieldPostContent.setText("")
                    binding.buttonAdd.isEnabled = true
                }

                is State.Failed -> {
                    showToast("Failed! ${state.message}")
                    binding.buttonAdd.isEnabled = true
                }
            }
        }

    }


    override fun onClick(v: View?) {
        when (v!!.id) {
            binding.buttonLoad.id -> {
                uiScope.launch {
                    loadPosts()
                }
            }
            binding.buttonAdd.id -> {
                uiScope.launch {
                    addPost(
                        Post(
                            postContent = binding.fieldPostContent.text.toString(),
                            postAuthor = "Karunesh Palekar"
                        )
                    )
                }
            }
        }


    }

    private  fun loadPosts() {
        viewModel.getallpostlivedata.observe(this, Observer { state->
           when(state){
               is State.Loading ->{
                   showToast("Loading")
               }
               is State.Success ->{
                   val postText = state.data.joinToString("\n") {
                       "${it.postContent} ~ ${it.postAuthor}"
                   }
                   binding.textPostContent.text = postText
               }
               is State.Failed ->{
                   showToast("Failed! ${state.message}")
               }
           }
        })
    }

    private fun showToast(message: String) {
        Toast.makeText(applicationContext, message, Toast.LENGTH_SHORT).show()
    }

}

谢谢您的帮助。

最佳答案

getAllPosts() 方法返回一个 Flow,它只加载您的帖子一次数据。它只会在您对该 Flow 调用 collect{} 时加载。

如果您想要 Cloud Firestore 的实时快照更新,那么您可以更新方法 getAllPosts(),如下所示。

fun getPostsRealtime() : Flow<State<List<Post>>> = callbackFlow {

    // Register listener
    val listener = addSnapshotListener { snapshot, exception ->

        offer(State.success(snapshot.toObjects(Post::class.java)))

        // If exception occurs, cancel this scope with exception message.
        exception?.let {
            offer(State.error(it.message.toString()))
            cancel(it.message.toString())
        }
    }

    awaitClose {
        // This block is executed when producer channel is cancelled
        // This function resumes with a cancellation exception.

        // Dispose listener
        listener.remove()
        cancel()
    }
}

在这里,我们使用 callbackFlow {},它允许我们异步使用回调方法和值发射。 希望对您有所帮助。

关于android - 从 Firebase 检索数据时协程流错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61696524/

相关文章:

android - 通过 USB 反向绑定(bind)多个 Android 设备

firebase - 警告 : The behavior for java. 存储在 Firestore 中的 util.Date 对象将会更改

android - Firebase:登录前写入数据库

firebase - Firebase Firestore 中的哨兵是什么?

android - 播放原始文件夹中的所有 mp3 文件

java - 想要执行某些操作以在 ListView 中单击的项目中查看

android - 如何使用 android 中的 aspose 库在 excel 工作表的列中插入值列表?

javascript - 刷新 vue.js 应用程序,无需硬重新加载

javascript - firebase.firestore.Blob.toBase64() "is not a function"..但是在文档中

android - 使用 Firestore 按键入的字母搜索