Hello guys, hope is this the right place for my question.
Is there a safe way to execute a lambda ( making sure that if my lambda outlived my fragment it will not leak a reference) ?
- I have a function that makes an API call in the background, if the user closes the fragment before that API request finishes -> I’ll have a leak, since my block (lambda) executes functions from the fragment (so it holds an implicit-reference to the fragment).
- The only way I was able to achieve what I want was by extending WeakReference<Fragment> for my lambda function
(like so:
fun WeakReference<Fragment>.safeBlock(block: WeakReference<Fragment>.() -> Unit)
)
And sending a the weak-reference-fragment as a parameter to my view model (which then sends it back to the lambda).