从 Firebase 检索数据时出现协程流错误

Kar*_*kar 2 android firebase google-cloud-firestore kotlin-coroutines

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

存储库

@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)


}
Run Code Online (Sandbox Code Playgroud)

国家级

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)

    }

}
Run Code Online (Sandbox Code Playgroud)

视图模型

@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)

}
Run Code Online (Sandbox Code Playgroud)

主要活动


@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()
    }

}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助。

Shr*_*til 6

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

如果您想要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()
    }
}
Run Code Online (Sandbox Code Playgroud)

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