takasek/Notifwift

Example code has retain cycle

Closed this issue · 2 comments

Thank you for providing this library to us. It looks helpful when I use Notifiacation(a.k.a. NSNotification) .

However I found a little mistake on your README. The section Real World Example tells me

The Notifwift instance is alive during MyViewController instance. It dies when MyViewController instance dies, so observers registered in the Notifwift instance are to be removed automatically. Never mind if you have to manage observer!

but this example code, MyViewController never dies, because you pass the function directory. This means function's owner captured by closure. You should do like this

notifwift.observe(didReceiveUserNotification) { [weak self] (user: User) in
    self?.userInfoView.reload(user)
}
  • Pass a closure (not func) to avoid capturing self.
  • Use [weak self] to avoid capturing self when you access IVar, actuary We must put self when we access IVar inside a closure though.

@griffin-stewie
It's exactly as you said.
I'd realized the mistake of my code(君の参照は。〜classとfunctionと循環参照の話〜 // Speaker Deck
), but forgotten to fix the README...
Thank you for pointing out.
I'm to fix the code as soon as possible.

I fixed the documents on both master & swift-2.3 branches.

74de31b 537a5d2

Thank you a lot!