Forum Xamarin.Forms

Issue with async calls in ViewModel causes crash on iOS but not on Android

I have an app where the first screen the user sees is a master detail page with a list and a menu. At least after the splash screen, that is.
The page is initialized as such:

var detailPage = (Page)this.componentContext.Resolve(pageType); // This is the page with the list
var page = (Page)this.componentContext.Resolve(typeof(MainPage)); // This is the menu page
var newPage = page as MasterDetailPage; 
newPage.Master = menuPage;
newPage.Detail = newDetailPage;
MainPage = newPage;
await this.InitializePage(detailPage); // Calls the ViewModel

public MasterDetailPage MainPage
        get => Application.Current.MainPage as MasterDetailPage;
        set => Application.Current.MainPage = value;

At this point, the ViewModel has been initialized and the following method in the ViewModel is called:

public override async Task InitializeAsync()
        if (!this.isInitialized)
            await this.eventBus.Send(new PopulateListEvent()); // This is the call that generates the error. Works on Android
            this.isInitialized = true;

The problem I face is when the await call is made, the app terminates on iOS (but not on Android) and I get a null reference exception with no inner text.
If I remove the await call, the page loads just fine, but with an empty list of course.

So it seems the problem lies within the way iOS deals with calls before the page is actually loaded.

Any ideas?

Sign In or Register to comment.