android - LiveData 的竞争条件
问题描述
TL;这个问题的博士:
用于和ArrayList的 ViewModel 内部的
LiveData
a是否可能具有竞争条件并且需要 using或 a ?前提是 ArrayList 将从回调中获取其值backing property (MutableLiveData)
Observe
Add
Synchronized
Lock
我正在尝试使用Agora Android SDK 设置群组视频通话。我按照这里的文档。问题出callbacks (onUserJoined, onUserOffline)
在IRtcEngineEventHandler 上。
OnUserJoined 回调
mRtcEngine = RtcEngine.create(baseContext, APP_ID, object : IRtcEngineEventHandler() {
override fun onUserJoined(uid: Int, elapsed: Int) {
// onUserJoined callback is called anytime a new remote user joins the channel
super.onUserJoined(uid, elapsed)
// We mute the stream by default so that it doesn't consume unnecessary bandwidth
mRtcEngine?.muteRemoteVideoStream(uid, true)
// We are using a lock since uidList is shared and there can be race conditions
lock.lock()
try {
// We are using uidList to keep track of the UIDs of the remote users
uidList.add(uid)
} finally {
lock.unlock()
}
onUserOffline 回调
override fun onUserOffline(uid: Int, reason: Int) {
// onUserOffline is called whenever a remote user leaves the channel
super.onUserOffline(uid, reason)
// We use toRemove to inform the RecyclerView of the index of item we are removing
val toRemove: Int
// We are using a lock since uidList is shared and there can be race conditions
lock.lock()
try {
// We are fetching the index of the item we are about to remove and then remove the item
toRemove = uidList.indexOf(uid)
uidList.remove(uid)
} finally {
lock.unlock()
}
这里Lock
是用来防止访问uidlist
的。当我完全遵循文档时,它对我有用,但是当我尝试使用 a和 a来保存 时,观察者总是返回一个空列表。thread safe
race condition
LiveData
backing property (MutableLiveData)
ViewModel
uidlist
uidlist
我的视图模型
class MainViewModel: ViewModel() {
private val _uidList: MutableLiveData<ArrayList<Int>> = MutableLiveData()
val uidList: LiveData<ArrayList<Int>> get() = _uidList
init {
_uidList.value = ArrayList<Int>()
}
fun addToUserList(uid: Int) {
_uidList.value?.add(uid)
Log.d("adding user ","$uid")
}
fun removeFromUserList(uid: Int) {
_uidList.value?.remove(_uidList.value!!.indexOf(uid))
}
}
我在addToUserList()
里面onUserJoined()
和removeFromUserList()
里面打电话onUserOffline()
请指导我解决这个问题,
谢谢
解决方案
你不应该改变存储在 中的值LiveData
,你会得到非常奇怪的行为。您必须完全交换价值。
我有点懒,所以我就给你答案。
class MainViewModel: ViewModel() {
private val _uidList: MutableLiveData<List<Int>> = MutableLiveData()
val uidList: LiveData<List<Int>> get() = _uidList
init {
_uidList.value = emptyList<Int>()
}
fun addToUserList(uid: Int) {
_uidList.value = (_uidList.value ?: emptyList()) + uid
Log.d("adding user ","$uid")
}
fun removeFromUserList(uid: Int) {
val value = _uidList.value?.toMutableList()
if (value == null) return
value.remove(value.indexOf(uid))
_uidList.value = value
}
推荐阅读
- tensorflow - 在 tensorflow 2.1.0 中使用什么代替 tf.contrib.layers.embed_sequence
- azure - Azure Log Analytics 和 App Insights 是否能够适应 Azure 区域内的数据中心故障
- ruby - 可以在 rspec 中使用花括号来阻止它吗?
- r - 如何使用 Tidymodels 获得 PCA 累积比例?
- java - 为什么运算符不给出不同的结果而是使用 = 给出正确的答案
- algorithm - 以下算法近似于什么?(假设 m>1,ε>0)
- angular - 如何在有限的资源和旧计算机上运行 Angular 构建
- ios - Objective-C 块:不兼容的块指针类型
- c# - 正则表达式匹配文件路径有或没有空格
- vue.js - vue如何设置不监听数据