【问题标题】:Coroutine Flow error while retrieving data from Firebase从 Firebase 检索数据时出现协程流错误
【发布时间】:2020-08-25 00:51:20
【问题描述】:

这是我第一次尝试使用 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)


}

状态类

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)

    }

}

视图模型

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

}

主活动


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

}

感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    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 {},它允许我们异步使用回调方法和值发射。 希望对您有所帮助。

    【讨论】:

    • 是的。我知道。将在 repo 中添加该代码供大家参考。让我知道上面的代码是否适合您。
    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多