FreshMvvm
FreshMvvm copied to clipboard
Implement same bevaviour as at PRISM IDestructible
During investigation different MVVM frameworks I found that FreshMvvm does not provide an ability to catch a moment when Page ViewModel become to destroy itself. It's very important to catch to provide a place for unsubscription from events, which must work during whole ViewModel life span. ViewIsAppearing
and ViewIsDisappearing
does not provide such ability cuz they are called each time when page loose focus, so we can't listen events when page at background.
Good example of such behaviour - IDestructible
interface from PRISM framework
I have the same issue, and am looking at ReverseInit(), which is supposed to be invoked when the page is popped from the navigation stack. However, I'm not seeing it get invoked when I pop the page using CoreMethods.PopPageModel(). I suspect that ReverseInit() is invoked not on the pagemodel that was popped, but on the pagemodel that pushed it. So, in that case, it isn't appropriate for disposing subscriptions.
I see that Prism's BaseViewModel implements IDestructible - see the bottom of this page: https://prismlibrary.com/docs/xamarin-forms/creating-your-first-prism-app.html#anatomy-of-a-prism-application
@yevgeny-sotnikov did you find out another way using FreshMVVM ?s
@rid00z Is there something like this? Would be great to have this as it's a very common use case.
I have the same issue, and am looking at ReverseInit(), which is supposed to be invoked when the page is popped from the navigation stack. However, I'm not seeing it get invoked when I pop the page using CoreMethods.PopPageModel(). I suspect that ReverseInit() is invoked not on the pagemodel that was popped, but on the pagemodel that pushed it.
ReverseInit()
needed to pass some result back to view-model, so it isn't applicable for our use-case.
@tele-bird unfortunatelly i still have this issue, no any proper ways to understand that page is destroying
Maybe the PageWasPopped event could suffice? Though it says that it might not be raised every time a page is popped, so probably not suitable to completely avoid memory leaks unless it is improved.
https://github.com/rid00z/FreshMvvm/blob/3ccf13b0b506b6e7193cdadde1d988ac76ac6889/src/FreshMvvm/FreshBasePageModel.cs
cc: @yevgeny-sotnikov , @rid00z
@yevgeny-sotnikov I recently discovered the FreshBasePageModel.PageWasPopped event, and I think it can be used for the important memory-conserving best-practices that you need. It is better than using ViewIsAppearing/ViewIsDisappearing because it only fires when you don't need it any more. Along with unsubscribing from events, I plan to use it to implement Disposable pattern, for IDisposable objects referenced by my pages. Hope this helps!
In my implementation, my base class unsubscribes from further notifications after the first one (line 10), since the page is quite unlikely to be popped a 2nd time!
public class BasePageModel : FreshBasePageModel
{
public BasePageModel(IConnectivity connectivityService)
{
PageWasPopped += HandlePageWasPopped;
}
private void HandlePageWasPopped(object sender, EventArgs e)
{
PageWasPopped -= HandlePageWasPopped;
OnPagePopped(sender, e);
}
protected virtual void OnPagePopped(object sender, EventArgs e)
{
}
}