This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
Reset cached bindings #7
Open
MichaelRocks
wants to merge
3
commits into
JakeWharton:master
Choose a base branch
from
MichaelRocks:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Seems this patch causes memory leaks so it shouldn't be merged. I'll try to find a better solution. |
The memory leak is fixed now. @JakeWharton, can you merge these commits please? |
Anybody alive here? |
without this change fragments cannot be used when the views are destroyed (e..g with page views ) |
I support this. I would like to see this merged too. @Suppress("UNCHECKED_CAST")
private fun <T : Any, V : View> required(id: Int, finder: T.(Int) -> View?)
= Lazy { t: T, desc -> t.finder(id) as V? ?: viewNotFound(id, desc) }
@Suppress("UNCHECKED_CAST")
private fun <T : Any, V : View> optional(id: Int, finder: T.(Int) -> View?)
= Lazy { t: T, desc -> t.finder(id) as V? }
@Suppress("UNCHECKED_CAST")
private fun <T : Any, V : View> required(ids: IntArray, finder: T.(Int) -> View?)
= Lazy { t: T, desc -> ids.map { t.finder(it) as V? ?: viewNotFound(it, desc) } }
@Suppress("UNCHECKED_CAST")
private fun <T : Any, V : View> optional(ids: IntArray, finder: T.(Int) -> View?)
= Lazy { t: T, desc -> ids.map { t.finder(it) as V? }.filterNotNull() }
// Like Kotlin's lazy delegate but the initializer gets the target and metadata passed to it
private class Lazy<T : Any, V>(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty<T, V> {
private object EMPTY
private var value: Any? = EMPTY
override fun getValue(thisRef: T, property: KProperty<*>): V {
if (value == EMPTY) {
value = initializer(thisRef, property)
LazyRegistry.register(thisRef, this)
}
@Suppress("UNCHECKED_CAST")
return value as V
}
internal fun reset() {
value = EMPTY
}
}
private object LazyRegistry {
private val lazyMap = WeakHashMap<Any, MutableCollection<Lazy<*, *>>>()
fun register(target: Any, lazy: Lazy<*, *>) {
lazyMap.getOrPut(target, { Collections.newSetFromMap(WeakHashMap()) }).add(lazy)
}
fun reset(target: Any) {
lazyMap.get(target)?.forEach { it.reset() }
}
}
object ButterKnife {
fun reset(target: Any) {
LazyRegistry.reset(target)
}
} |
We need this functionality to correctly implement Fragments being detached and re-attached (ex: ViewPager). |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #5 by keeping a registry of cached lazies and resetting them on purpose.