Friday 4 October 2019

c# - 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.


Answer



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
}
}
}
}


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...