Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear handler? #28

Open
MKGitHub opened this issue Oct 21, 2017 · 8 comments
Open

Clear handler? #28

MKGitHub opened this issue Oct 21, 2017 · 8 comments

Comments

@MKGitHub
Copy link

Example

button.onTap(handler:{
    // do stuff
})

How do I clear this handler to avoid a leak when my VC is deallocated?

@vhesener
Copy link
Owner

See here: https://stablekernel.com/how-to-prevent-memory-leaks-in-swift-closures/#toc_2.

button.onTap(handler:{ [weak self] in
    self?.doStuff()
})

@MKGitHub
Copy link
Author

So there is no way to clear the handler on the button?

@vhesener
Copy link
Owner

vhesener commented Oct 22, 2017

If you can help me understand the problem, I'll be able to help you and also help myself in order to provide the best solution for your (and other's) use case.

It just simply uses target-action, so everything is weakly held. The closure will stay around as long as you have the button strongly held by your VC, just the way that a target-action handler stays around when you use @IBAction or something.

If all you want to do is remove the handler, there are a few ways to do that right now as a work-around:

button.removeTarget(nil, action: nil, for: .touchUpInside) //removes all touch up inside events

button.onTap {} //sets the handler to do nothing

Let me know if this works or maybe what you're trying to accomplish and I'll try to assist as best I can.

@smiLLe
Copy link

smiLLe commented Oct 24, 2017

i think he is talking about the .removeTarget.
for example
button.onTap { [weak self] in self?.loadData() } button.onTap { [weak self] in self?.loadData() }

if we tap the button, we load the data twice, right?

@vhesener
Copy link
Owner

Ah ok. Yes, I just saw this bug yesterday and fixed it. Please upgrade to v0.3, as it is fixed there.

I will leave this issue open, however, because I think it would be beneficial to have an explicit way to remove the handlers.

Good find, and thanks for the feedback!

@MKGitHub
Copy link
Author

Actually I was saying if I can do this

button.onTap(handler:{
    // do stuff
})

then I would like to do this

button.removeOnTapHandler()

@nikmoiseev
Copy link

Hello
Any news on this?
I would like the block to be deleted when the object is deleted without additional code

@vhesener
Copy link
Owner

vhesener commented Aug 1, 2019

Objects are removed without additional code. This issue is for an enhancement to have it explicitly removed. If you are having those types of issues you'll have to post a new issue with your code so we can take a look. Likely it may be a retain cycle in your closure handler (retaining self typically).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants