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.
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.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)