简体   繁体   English

MonoTouch中的弱事件模式

[英]Weak Event Pattern in MonoTouch

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. 我曾经使用Objective-C语言开发iOS应用程序,并依赖dealloc方法在我的应用程序中执行一些清理/取消注册任务。 Now on the MonoTouch (garbage collected) it is not an option anymore. 现在在MonoTouch(垃圾收集)上它不再是一个选项。

Suppose I have a UIViewController that adds as a subview of it's View property an instance of MyView ( UIView subclass). 假设我有一个UIViewController ,它将其View属性的子视图添加为MyViewUIView子类)的实例。 MyView in turn registers itself to receive some events from another manager/global object so that it knows how to update itself accordingly (eg: onlineProfilesManager.Refreshed += () => <update UI with the new state>; ). MyView反过来注册自己从另一个管理器/全局对象接收一些事件,以便它知道如何相应地更新自己(例如: onlineProfilesManager.Refreshed += () => <update UI with the new state>; )。

As long as MyView is on screen, everything is fine. 只要MyView在屏幕上,一切都很好。 However I must know when it's removed from the screen so that I can unregister MyView from the event handler. 但是我必须知道它什么时候从屏幕上删除,以便我可以从事件处理程序中取消注册MyView

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. 在Obj-C中,这可以简单地在dealloc方法中完成,因为当屏幕改变时, UIViewController被释放 - > MyView从它的MyView中删除,然后调用MyView dealloc方法。

In Monotouch I don't have this 'deterministic' flow anymore. 在Monotouch中,我不再拥有这种“确定性”流程。 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). 我试图在UIViewControllerMyView析构函数中放置一些print语句,但它们永远不会被调用(原因是因为MyView仍然为事件处理程序注册,因为我不知道何时/如何取消注册它,它永远不会解除分配)。

Does anyone know what is the 'pattern' to handle such situations in MonoTouch? 有谁知道在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. 编辑我正在编辑我的问题,因为看起来像我的问题的解决方案是使用弱事件模式,但我没有找到MonoTouch平台的实现。

Does anyone know how can I use the Weak Event Pattern in MonoTouch ? 有谁知道如何在MonoTouch中使用弱事件模式

The best way to handle events is to unregister them in ViewWillDisappear and register them in ViewWillAppear. 处理事件的最佳方法是在ViewWillDisappear中取消注册它们,并在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/ 如果这不符合您的需求,您可以执行与此类似的操作http://sgmunn.com/blog/2012/05/non-gcd-event-handlers/

Cheers. 干杯。

If you are looking for weak events, you can try my "Messenger" implementation here . 如果您正在寻找弱事件,可以在这里尝试我的“Messenger”实现。

It is inspired by what is available in TinyIoC , but I re-implemented it so it used less reflection, etc. 它的灵感来自TinyIoC中的可用内容 ,但我重新实现了它,因此它使用较少的反射等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM