4

I used to develop iOS apps using the Objective-C language, and relied on the dealloc method to perform some cleanup/unregister tasks in my application. Now on the MonoTouch (garbage collected) it is not an option anymore.

Suppose I have a UIViewController that adds as a subview of it's View property an instance of MyView (UIView subclass). MyView in turn registers itself to receive some events from another manager/global object so that it knows how to update itself accordingly (e.g.: onlineProfilesManager.Refreshed += () => <update UI with the new state>;).

As long as MyView is on screen, everything is fine. However I must know when it's removed from the screen so that I can unregister MyView from the event handler.

In Obj-C this could be simply done in the dealloc method because when the screen changes the UIViewController is deallocated --> MyView is removed from it's superview and then MyView dealloc method is called.

In Monotouch I don't have this 'deterministic' flow anymore. I tried to put some print statements in the UIViewController and MyView destructors but they are never called (the reason is because the MyView is still registered for the event handler, since I don't know when/how to unregister it, it will never be deallocated).

Does anyone know what is the 'pattern' to handle such situations in MonoTouch? I think I'm missing a fundamental concept and getting into trouble developing my apps.

Thanks in advance.


EDIT I'm editing my question because looks like the solution for my problem is using the Weak Event Pattern but I didn't find an implementation for the MonoTouch platform.

Does anyone know how can I use the Weak Event Pattern in MonoTouch ?

Rolf Bjarne Kvinge
  • 19,253
  • 2
  • 42
  • 86
Eduardo Coelho
  • 1,953
  • 8
  • 34
  • 60
  • 4
    Try overriding `Dispose`. This might not work for your case though (the way it sounds). Let me know. – jonathanpeppers Feb 25 '13 at 23:58
  • It might not work if he overrides Dispose on MyView, but it should work if he disconnects the handler in MyViewController's Dispose override. – jstedfast Feb 26 '13 at 13:58
  • @jstedfast @Greg Munn (below) suggested for me to disconnect/connect the event handler in the `ViewWillDisappear` and `ViewWillAppear` method pair. A similar situation would be to perform this on the `Constructor` and the `Dispose` methods? Will `Dispose` be invoked if `UIViewController` was captured by the event handler? – Eduardo Coelho Feb 26 '13 at 14:40

2 Answers2

6

The best way to handle events is to unregister them in ViewWillDisappear and register them in ViewWillAppear. This means that you can't use anonymous methods though as you don't have a reference to the method to unregister it.

If that doesn't suit what you need, you can do something similar to this http://sgmunn.com/blog/2012/05/non-gcd-event-handlers/

Cheers.

Greg Munn
  • 526
  • 2
  • 7
  • `ViewWillAppear` and `ViewWillDisappear` sounds interesting but what if a ViewController wants to present a Modal View Controller (i.e.: to retrieve user information) and receive the results using an event handler? At the time the Modal View Controller is presented the root view controller will have it's `ViewWillDisappear`method invoked and thus will unregister itself for such events. That's the main point: I don't know which method to override to perform register/unregister tasks – Eduardo Coelho Feb 26 '13 at 21:18
  • `ViewDidUnload` is also not an option (its obsolete and isn't guaranteed to be always called when the View controller is closed) – Eduardo Coelho Feb 26 '13 at 21:21
  • 1
    @EduardoCoelho View A presents a view B modally -> In this case you'd have to keep a flag to know if B is modal. You can also check the value of PresentedViewController (it'll be the modal one). – Greg Munn Feb 26 '13 at 23:48
  • I created a gist with a abstract UIViewController subclass that serves as a 'pattern' for registering/unregistering for events in UIViewController subclasses. https://gist.github.com/eduardocoelho/5044439 what do you think? – Eduardo Coelho Feb 27 '13 at 02:26
1

If you are looking for weak events, you can try my "Messenger" implementation here.

It is inspired by what is available in TinyIoC, but I re-implemented it so it used less reflection, etc.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182