```fun clearKey(key: Any?) { val entry = entri...
# android
m
Copy code
fun clearKey(key: Any?) {
    val entry = entries[key] ?: return
    entry.isDisposable = true
    if (entry.isDisposable && entry.refCount <= 0) {
        remove(key)
    }
}

public override fun onCleared() {
    entries.toScatterMap().forEachValue { entry ->
        entry.isDisposable = true
        if (entry.refCount <= 0) {
            remove(entry.key)
        }
    }
}
What’s the difference between
entry.isDisposable && entry.refCount <= 0
and
entry.refCount <= 0
?
The code is from ViewModelProvider.kt
t
I do not know the original intended behaviour, but in practice there's no difference, because
Copy code
entry.isDisposable = true
    if (entry.isDisposable && entry.refCount <= 0) {
        remove(key)
    }
appears to be a very redundant check on
entry.isDisposable
, so both expressions should probably be
entry.refCount <= 0
only.
b
here it is being checked whether resource associated with that key is disposable or not followed by no more reference to the element being represented by that particular key, ideally this should not cause any problem but in multi-threaded environment these double checks become essential to safeguard before removal of key. I hope this makes some sense
t
but in multi-threaded environment these double checks become essential to safeguard before removal of key.
I'm trying to spin in my head how that would be a valid safety check in that context.
Copy code
entry.isDisposable = true
if (entry.isDisposable 
&& // <-- Couldn't a mutation happen here on `isDisposable`? 
entry.refCount <= 0) {
    remove(key)
}
I'd tend to believe it is either very redundant or very unreliable way of achieving safety, but I am very new to Android ecosystem and Kotlin in general, so maybe there's something I'm missing?
🦗 1