rubymotion-community / BubbleWrap

Cocoa wrappers and helpers for RubyMotion (Ruby for iOS and OS X) - Making Cocoa APIs more Ruby like, one API at a time. Fork away and send your pull requests
Other
1.18k stars 208 forks source link

Added BubbleWrap.use_weak_callbacks, default to false. #324

Closed hboon closed 10 years ago

hboon commented 10 years ago

When set to true, #weak! will be used for UIControlWrapper.

Also: https://github.com/rubymotion/BubbleWrap/issues/322#issuecomment-30558903.

Added #use_weak_callbacks to shortcut.rb, seeing that #debug is there. Is it the correct place?

clayallsopp commented 10 years ago

Sweet - is it possible to add a test for this/the two instances where weak! is called?

hboon commented 10 years ago

First-timer with spec, would be safe if someone help reviews the test code. Also duplicated the test case across 2 files, not sure the alternative of extracting them out is good.

clayallsopp commented 10 years ago

Looks good, but did you mean to use when_tapped instead of subject.when(UIControlEventTouchUpInside) in ui_view_wrapper_spec.rb?

ryansobol commented 10 years ago

Pardon my ignorance, but why is this setting necessary? Aren't blocks that create retain cycles bugs?

UIControl#when and friends all hold a strong reference to a block callback. So when would a view controller, a view, or any other object, want this block -- defined in its scope -- to hold a strong reference to itself?

EDIT: Forgot to explicitly refer to a BW callback.

hboon commented 10 years ago

It's a bug, but I suppose some people might have inevitably relied on the strong reference. So it'd be nice to keep the behaviour, preserve compatibility and let people switch manually for the time being.

I defer to Clay here: https://github.com/rubymotion/BubbleWrap/issues/322#issuecomment-30558903

clayallsopp commented 10 years ago

Saw the test was fixed, thanks @hboon!