xamarin.formsfreshmvvm

FreshMvvm lifecycle issue


I have a xamarin.forms app using FreshMvvm. The following method in App.xaml.cs handles a push notification:

   public static async void NewCall(string message)
    {
        ...
        await Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
        {          
            if (!IsVideoCallViewOpen)
            {          
                var navService = FreshIOC.Container.Resolve<IFreshNavigationService>(FreshMvvm.Constants.DefaultNavigationServiceName);
                Page videoCallPage = FreshPageModelResolver.ResolvePageModel<VideoCallPageModel>();
                await navService.PushPage(videoCallPage, null);
            }

            VideoCallMessage caller = JsonConvert.DeserializeObject<VideoCallMessage>(message);
            VideoCallPeer peer = new VideoCallPeer(caller.UserId, caller.UserName, caller.FirstName);

            OnStartCall?.Invoke(null, peer);
        });
    }

As you can see, navService.PushPage() opens a certain VideoCallPage, if needed. Only after that OnStartCall event should be fired. But it does not happen this way. It fires BEFORE VideoCallPageModel's Init() method finishes execution, which causes various troubles. How can I make sure OnStartCall event doesn't fire before the VideoCallPage is done loading?

Here is the VideoCallPageModel's Init signature:

public async override void Init(object initData)

And here is VideoCallPageModel's StartCallHandler() that handles OnStartCall event:

public void StartCallHandler(object sender, VideoCallPeer remotePeer)

Solution

  • Have you thought about using Messaging Center?

    Messaging Center Documentation

    You could send a message once you are sure you are ready and then whatever is subscribed to it, will do the work for you once the Message has been received.