architecture-samples icon indicating copy to clipboard operation
architecture-samples copied to clipboard

Make it possible to cancel a running use case in todo-mvp-clean

Open ErikHellman opened this issue 10 years ago • 5 comments

UseCaseHandler.execute() needs to return an object that lets us cancel an ongoing task. This is to prevent the callback from leaking a reference to the view. The cancel operation doesn't need to abort the thread, but should basically clear the reference to the callback to avoid the leak. See #154

ErikHellman avatar Aug 24 '16 19:08 ErikHellman

I solved that by returning a Subscription(direct copy from RxJava) which UseCase implements. e.g.,

Subscription subscription = UseCaseHandler.execute(doFoo, ...);

~~where UseCaseHandler.execute() returns UseCase itself internally.~~ Don't do that this leaks callback reference

gokhanbarisaker avatar Aug 24 '16 22:08 gokhanbarisaker

I've reviewed the code and we are leaking the callback reference, my fault. I don't like to create a cancel method for avoiding this one because is move the decision to the developer and it's easy to forget it. My idea is to take weak references and not a strong reference for the callback, this will and auto cancel for those calls that don't have a callback attached.

I will add a pull request during this weekend.

flipper83 avatar Aug 25 '16 07:08 flipper83

@flipper83 isn't it risky to trust object to be garbage collected from memory to unsubscribe from callbacks? and most likely leak-prune

gokhanbarisaker avatar Aug 25 '16 14:08 gokhanbarisaker

Yeah, have to agree with @gokhanbarisaker. A weak reference is not very optimal. We already have subscribe/unsubscribe in other samples, so it should be fine to use that here as well. The view tells the presenter when it is being detached.

ErikHellman avatar Aug 25 '16 15:08 ErikHellman

Closing as obsolete in the context of the current architecture guide.

tunjid avatar Mar 11 '23 06:03 tunjid