【发布时间】:2019-06-04 09:00:58
【问题描述】:
我有一张地图,在我的例子中是ConcurrentHashMap<String, Device>,当在 websocket 上接收到一些事件时它正在更新。我想在这个地图上实现一个 observable,以了解何时添加、更新或删除条目。我尝试使用ObservableProperty,但地图更改时没有调用任何方法。
var deviceCache : ConcurrentHashMap<String, Device> by MyObservable(ConcurrentHashMap())
class MyObservable<T, V>(initialValue: ConcurrentHashMap<T, V>) : ObservableProperty<ConcurrentHashMap<T, V>>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: ConcurrentHashMap<T, V>, newValue: ConcurrentHashMap<T, V>) {
super.afterChange(property, oldValue, newValue)
log.e("new value is $newValue")
}
override fun beforeChange(property: KProperty<*>, oldValue: ConcurrentHashMap<T, V>, newValue: ConcurrentHashMap<T, V>): Boolean {
log.e("beforeChange called")
return super.beforeChange(property, oldValue, newValue)
}
}
谁能帮我解决这个问题?
【问题讨论】:
-
MyObservable长什么样子? -
@AdamArold 这是我的问题。
-
哦,抱歉我看到了
标签: dictionary kotlin delegates observable concurrenthashmap