gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:13:36 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com