Seafish
Seafish

Reputation: 2271

Xamarin Async ViewDidAppear called during ViewDidLoad

I'm trying to initialize a view model on ViewDidLoad. I need to call some async methods in the ViewModel initialization code, so I've moved the async code out of the constructor into an async factory method.

I've marked the ViewDidLoad and ViewWillAppear as async void in my UIViewController subclass, but for some reason while line 4 is executing the ViewWillAppear is kicked off and line 11 throws a NullReferenceException because the ViewModel isn't initialized yet.

My suspicion is that Xamarin can't wait for ViewDidLoad to complete because it's async void, but I have to use an async void here because it's overriding a method.

MyCustomUiViewController.cs

1  public override async void ViewDidLoad()
2  {
3      base.ViewDidLoad();
4      ViewModel = await ViewModel.CreateAsync();
5      OtherStuff();
6  }
7 
8  public override async void ViewWillAppear(bool animated)
9  {
10     base.ViewWillAppear(animated);
11     ViewModel.SomeMethod(); // <-- NullReferenceException
12     AttachViewModelToViewBindings();
13 }

I'm open to changing the architecture if there is a better pattern for instantiating an async ViewModel.

Upvotes: 3

Views: 3324

Answers (3)

Lingfei Zhu
Lingfei Zhu

Reputation: 1

Yes, when you mark ViewDidLoad as async, UI thread will not wait for ViewDidLoad as a whole (inside the ViewDidLoad function it will wait as long as you put await keyword). So UI thread will go to ViewWillAppear very fast(while your async web request inside the ViewDidLoad is still executing), thus ViewModel is still null.

Upvotes: 0

Seafish
Seafish

Reputation: 2271

Here's the generalized pattern that we used (extracted into this gist). This lets you create a controller that inherits from AsyncInitializationController and then overrides, for example, ViewDidLoadAsync. The code is structured so that each subsequent lifecycle method waits for the previous ones to complete.

While we didn't have a need for an async ViewDidDisappear, I'm sure you could work that into this pattern as well.

using System;
using System.Threading.Tasks;
using UIKit;

namespace Seanfisher.Gists
{
    public abstract class AsyncInitializationController : UIViewController
    {
        Task _viewDidLoadAsyncTask = Task.CompletedTask;
        public virtual Task ViewDidLoadAsync()
        {
            return _viewDidLoadAsyncTask;
        }

        public sealed override async void ViewDidLoad()
        {
            try
            {
                base.ViewDidLoad();
                _viewDidLoadAsyncTask = ViewDidLoadAsync();
                await _viewDidLoadAsyncTask;
            }
            catch (Exception e)
            {
                // Handle
            }
        }

        Task _viewWillAppearAsyncTask = Task.CompletedTask;
        public virtual Task ViewWillAppearAsync()
        {
            return _viewWillAppearAsyncTask;
        }

        public sealed override async void ViewWillAppear(bool animated)
        {
            try
            {
                await _viewDidLoadAsyncTask;
                base.ViewWillAppear(animated);
                _viewWillAppearAsyncTask = ViewWillAppearAsync();
                await _viewWillAppearAsyncTask;
            }
            catch (Exception e)
            {
                // Handle
            }
        }

        Task _viewDidAppearAsyncTask = Task.CompletedTask;
        public virtual Task ViewDidAppearAsync()
        {
            return _viewDidAppearAsyncTask;
        }
        public sealed override async void ViewDidAppear(bool animated)
        {
            try
            {
                await _viewDidLoadAsyncTask;
                await _viewWillAppearAsyncTask;

                base.ViewDidAppear(animated);
                _viewDidAppearAsyncTask = ViewDidAppearAsync();
                await _viewDidAppearAsyncTask;
            }
            catch (Exception e)
            {
                // Handle
            }
        }
    }
}

Upvotes: 4

Norrin
Norrin

Reputation: 385

Bodangly is right.

The methods wont be called asynchrounsly just because you mark them async.

Furthermore - "async void" should always be avoided. Read this: https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

A better pattern to solve this is explained here: http://blog.stephencleary.com/2013/01/async-oop-2-constructors.html

Which should be something like this: (Untested)

public override void ViewDidLoad()
{
   base.ViewDidLoad();
   Initialization = InitializeAsync();

   OtherStuff();
}

public Task Initialization { get; private set; }

private async Task InitializeAsync()
{
    // Do our own initialization (synchronous or asynchronous).
    await Task.Delay(100);
}

Upvotes: -2

Related Questions