Skip to content

[BUG] Popup.CloseAsync causing a TargetInvocationException (UIThread) on iOS #2509

@Sebastian1989101

Description

@Sebastian1989101

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

It seems like a Popup.CloseAsync() call can cause a UIThread issue when running on iOS. It runs fine on Android and if anything, I would assume this exception already when trying to display the Popup (as this is in the same async Task method in my case).

Expected Behavior

Popup.CloseAsync() makes sure it runs on the proper thread.

Steps To Reproduce

Not sure how much I would need to remove from my code to make this happen. This method is invoked from the constructor of the page:

    public async Task LoadDataWithKey(string? key)
    {
        LoadingPopup? loadingPopup = null;

        try
        {
            loadingPopup = new LoadingPopup { Message = AppResources.DefaultText_Loading };
            Shell.Current.ShowPopup(loadingPopup);

            var allCategories = await speedrunCategoryDatabaseRepository.GetAllAsync();
            Category = allCategories.First(c => c.Key == key)!;

            Entries = (await apiRepository.GetEntriesAsync(_category).ConfigureAwait(false))
                .Select(e => new SpeedrunEntryWrapperViewModel(e)).ToArray();
        }
        catch (Exception e)
        {
            logger.LogError(e, e.Message);
        }
        finally
        {
            if (loadingPopup != null)
            {
                await loadingPopup.CloseAsync();
                loadingPopup.DisposeIfDisposable();
            }
        }
    }

Link to public reproduction project repository

n/a

Environment

- .NET MAUI CommunityToolkit: 11.0.0
- OS: Windows 11 / iOS 16 as Debug Target
- .NET MAUI: 9

Anything else?

I know that I did not attached a reproduction project because I do not know yet how to isolate the issue so far that it's an issue. I just found out that on iOS I have to sent the Popup.CloseAsync() to the MainThread manually for my app to no longer crash with this exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/viewsIssue/Discussion/PR that has to do with ViewsbugSomething isn't workingunverified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions