iosxamarinmaui

Maui application throws UITableView error after navigating to certain pages


I'm currently developing a MAUI application for Android and iOS. When deploying the app to an iOS device - regardless if it's a simulator or a real device - there are a few content pages in my application that will throw the following error when navigating to them:

enter image description here

Googling the issue has led me to believe that the error occurs when you try to asynchronously fill or update views that have not been drawn yet. To be honest, I did try to load data in the Appearing event in my pages, but I already fixed that. Please have a look at the following sample. This is an example of a page called StocktakingView and its view model that will throw the error:

<ContentPage xmlns=...>
<ContentPage.Behaviors>
    <toolkit:EventToCommandBehavior EventName="Appearing" Command="{Binding AppearingCommand}" />
    <toolkit:EventToCommandBehavior EventName="NavigatedFrom" Command="{Binding NavigatedFromCommand}" />
</ContentPage.Behaviors>
<ContentPage.ToolbarItems>
    <ToolbarItem Command="{Binding CreateCommand}" IconImageSource="{FontImageSource FontFamily=MaterialSharp, Glyph={x:Static m:MaterialSharp.Add}, Color={AppThemeBinding Dark=White, Light=Black}}" />
</ContentPage.ToolbarItems>
    <CollectionView ItemsSource="{Binding Stocktakings}">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="model:Stocktaking">
                <Grid ColumnDefinitions="*,*" Padding="10">
                    <Grid.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type vm:StocktakingViewModel}}, Path=TapCommand}" CommandParameter="{Binding .}" />
                    </Grid.GestureRecognizers>
                    <Label Grid.Column="0" Text="{Binding Name}" FontAttributes="Bold" />
                    <Label Grid.Column="1" Text="{Binding Status}" />
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage>

And this is the view model:

[QueryProperty("Stocktakings_list", "stocktakings")]
public partial class StocktakingViewModel : BaseViewModel
{
    private readonly IUserDialogs _userDialogs;
    private readonly IGenericService<Stocktaking, StocktakingCreationInfo> _stocktakingService;

    [ObservableProperty]
    List<Stocktaking> stocktakings_list;

    [ObservableProperty]
    ObservableCollection<Stocktaking> stocktakings;

    public StocktakingViewModel(IUserDialogs userDialogs, IGenericService<Stocktaking, StocktakingCreationInfo> stocktakingService)
    {
        _userDialogs = userDialogs;
        _stocktakingService = stocktakingService;
    }

    [RelayCommand]
    private void OnAppearing()
    {
        Stocktakings = new ObservableCollection<Stocktaking>(Stocktakings_list);
    }
...

Stocktakings is a list of objects which get loaded by a HTTP request when tapping on an image in the previous page. This happens before navigating to the Stocktaking page. The list then gets passed as a parameter for the StocktakingView.

private async Task NavigateToStocktaking()
{
    List<Stocktaking> stocktakings = await _stocktakingService.GetAll();

    Dictionary<string, object> param = new Dictionary<string, object>
    {
        { "stocktakings", stocktakings }
    };

    await Shell.Current.GoToAsync(nameof(StocktakingView), param);
}

There are other pages in my app which also have collection views in them and which do not throw any error. Those views are also filled with data before navigating to the page. There is basically no difference between those pages and the faulty pages, yet they do not throw any error.

What I already tried:

I'm currently developing with Visual Studio 17.9.2 on a Windows PC. I'm connected to my Mac which has XCode 15.2 installed. I'm also using the Community MVVM toolkit.


Solution

  • Found the issue. It has nothing to do with populating the view but rather with the service. My service throws an exception if IsSuccessStatusCode is false and that led to the exception you can see in the screenshot. The wording of the exception made me believe that it had something to do with populating or drawing the collection view and it made me not notice the service exception. Since I cannot set any breakpoints when debugging iOS, I never had the opportunity to actually test the service and I just assumed it worked. However, the issue had nothing to do with MAUI being buggy.