Mediator is not being destroyed when View is removed

39 views
Skip to first unread message

Jason Bou Kheir

unread,
Sep 23, 2019, 5:04:17 PM9/23/19
to StrangeIoC
Hi there,

I think I have a misunderstanding with what the Mediator is supposed to do when a View is removed. The method calling Mediator.OnRemove is titled "DestroyMediator", implying that the mediator should be destroyed. However, when I remove a View component from a Gameobject, the Mediator is left dangling without a view--not destroyed.

I can see cases where you wouldn't want a Mediator to be immediately destroyed. For example, in SignalMediationBinder to clean up the [ListensTo] delegates.


1. Is the Mediator supposed to be destroyed when the View is removed?
2. If so, do you have any recommendations for patching to ensure that the proper cleanup in SignalMediationBinder (or any future inherited Binder) occurs?


Thanks for reading!

Cheers,
Jason

Will Corwin

unread,
Sep 23, 2019, 5:25:32 PM9/23/19
to Jason Bou Kheir, StrangeIoC
A mediator should be destroyed, on remove should be called, and listensTo should be all cleaned up when a view is destroyed. Could you give more information about what you're doing? Can you confirm that the AbstractMediationBinder.Trigger( MediationEvent.DESTROYED, view) is being called when you destroy your view?

--
You received this message because you are subscribed to the Google Groups "StrangeIoC" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strangeioc+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/strangeioc/c22ca4de-3166-40a1-a1a1-159ca220ef76%40googlegroups.com.

Jason Bou Kheir

unread,
Sep 23, 2019, 6:14:48 PM9/23/19
to StrangeIoC
Sure!


I can confirm that the AbstractMediationBinder.Trigger(MediationEvent.Destroyed, view) is being called with the correct view instance.

On Monday, September 23, 2019 at 2:25:32 PM UTC-7, wcorwin wrote:
A mediator should be destroyed, on remove should be called, and listensTo should be all cleaned up when a view is destroyed. Could you give more information about what you're doing? Can you confirm that the AbstractMediationBinder.Trigger( MediationEvent.DESTROYED, view) is being called when you destroy your view?

On Mon, Sep 23, 2019 at 2:04 PM Jason Bou Kheir <ja...@karunavr.com> wrote:
Hi there,

I think I have a misunderstanding with what the Mediator is supposed to do when a View is removed. The method calling Mediator.OnRemove is titled "DestroyMediator", implying that the mediator should be destroyed. However, when I remove a View component from a Gameobject, the Mediator is left dangling without a view--not destroyed.

I can see cases where you wouldn't want a Mediator to be immediately destroyed. For example, in SignalMediationBinder to clean up the [ListensTo] delegates.


1. Is the Mediator supposed to be destroyed when the View is removed?
2. If so, do you have any recommendations for patching to ensure that the proper cleanup in SignalMediationBinder (or any future inherited Binder) occurs?


Thanks for reading!

Cheers,
Jason

--
You received this message because you are subscribed to the Google Groups "StrangeIoC" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stran...@googlegroups.com.

Andy Riedel

unread,
Sep 23, 2019, 6:19:15 PM9/23/19
to StrangeIoC
To clarify, Jason has found that the Mediator Component is never destroyed (via it's MonoBehaviour.OnDestroy). The bubbling and call to the Mediator's OnRemove work but then Mediator Components is left attached to the GameObject.

Will Corwin

unread,
Sep 23, 2019, 7:10:13 PM9/23/19
to Andy Riedel, StrangeIoC
Wow, this is just a bug. Thanks! I guess it's possible we've all just been destroying game objects and not components. The fix is straightforward but I don't have much time to do thorough testing (I have a newborn, woo!). 

Head to MediationBinder.DestroyMediator, snag the component, and destroy it if it exists. If it's an IMediator, call OnRemove. That should be all.

Just to be clear, you're seeing the signal mediation binder handle the delegates, right? I was seeing everything else I expect, just not the destruction of the mediator.

If you could implement, test it and issue a PR, I'll approve. We can't test this bit, as well. Because it's UnityEngine.Destroy, but any unit tests you can think of would be great. Thanks again.

--
You received this message because you are subscribed to the Google Groups "StrangeIoC" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strangeioc+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/strangeioc/0a33d1f9-bbfd-4f3b-8e38-57f2e7c07695%40googlegroups.com.

Andy Riedel

unread,
Sep 23, 2019, 8:09:40 PM9/23/19
to Will Corwin, StrangeIoC
Hi Will,

Congratulations on the baby and thank you for being awake enough to respond! We really appreciate it!

Yes, we are seeing SignalMediationBinder properly handling the ListensTo delegates.

Here was our patch. Let me know if you think there is a cleaner solution, if not I'll issue a PR with this.

Changes to MediationBinder.DestroyMediator: 
  (1) Call UnityEngine.Object.Destroy on the IMediator after calling OnRemove by casting it as a Component. Always return null since the mediator is dead at this point...

/// Destroy the Mediator on the provided view object based on the mediatorType
protected override IMediator DestroyMediator(IView view, Type mediatorType)
{
var mono = view as MonoBehaviour;
IMediator mediator = mono.GetComponent(mediatorType) as Mediator;

if (mediator != null)
{
mediator.OnRemove();
Object.Destroy((Component) mediator);
}

return null;
}
Changes to SignalMediationBinder.DestroyMediator: 
  (1) Copy paste everything from MediationBinder.DestroyMediator: up to the IMediator.OnRemove
  (2) Handle the delegates
  (3) Call UnityEngine.Object.Destroy on the IMediator after calling OnRemove by casting it as a Component. Always return null since the mediator is dead at this point...

/// Manage Delegates, then remove the Mediator from a View
protected override IMediator DestroyMediator(IView view, Type mediatorType)
{
var mono = view as MonoBehaviour;
IMediator mediator = mono.GetComponent(mediatorType) as Mediator;

if (mediator != null)
{
mediator.OnRemove();

HandleDelegates(mediator, mediatorType,false);

Object.Destroy((Component)mediator);
}

return null;
}
Reply all
Reply to author
Forward
0 new messages