aleclarson / emitter-kit

Type-safe event handling for Swift
MIT License
567 stars 71 forks source link

Not receiving event when using 'on' #31

Closed ashmore11 closed 8 years ago

ashmore11 commented 8 years ago

Hi there,

I'm trying to emit an event every time I update my array so I can reload a specific row in my table view. So far in other areas of my app I've been using 'once' without any problems but now I need to use 'on' instead of 'once' and for some reason it's not working...

I'm probably just missing something small here but I just can figure it out.

Hope you can help, Thanks

aleclarson commented 8 years ago

Did you run the tests to see if they're passing for you?

danielrhodes commented 8 years ago

I've been having the same issue. once works fine.

danielrhodes commented 8 years ago

@ashmore11 For whatever reason, if you retain the returned Listener from on somewhere it works. Not sure why there would be any difference with once, but if you need something that works now that's it.

@aleclarson Why would that be?

aleclarson commented 8 years ago

From the Listener wiki page:

Remember to retain a Listener if once == false! Make a [Listener] property and put it there.

For convenience, it's assumed that a Listener created with the once() method will definitely be called. Thus, it's not required to retain the Listener. But if you ever need to stop the Listener before it receives the event, you should retain the Listener so you can call its stopListening() method when necessary.

A Listener created with the on() method may never be called. Thus, you must retain the Listener for it to continue receiving events. This means your class can retain an array of Listeners; then whenever your class is deallocated, the array of Listeners is automagically cleaned up!

Let me know if you have more questions!

yvbeek commented 8 years ago

@aleclarson thank you for explaining the difference in retaining the listener between on and once. I was working with Notifications and the wiki page confused me a bit:

You do not need to retain a Notification for your listener to work correctly. This is one reason why Notification does not subclass Emitter.

It might be useful to add the "Remember to retain a Listener if once == false!" to that page as well.

aleclarson commented 8 years ago

@Zyphrax Good idea! Thanks! 👍