Skip to content

Don’t call NSAttributedString with HTML from a background thread #26153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2025

Conversation

kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Nov 27, 2024

According to Apple's documentation: https://developer.apple.com/documentation/foundation/nsattributedstring/1524613-initwithdata

Don’t call this method from a background thread if the options dictionary includes the NSDocumentTypeDocumentAttribute attribute with a value of NSHTMLTextDocumentType. If you do, the method tries to synchronize with the main thread, fails, and times out. Calling it from the main thread works, but can still time out if the HTML contains references to external resources. The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import.

Also, it seems to be a common problem among Apple developers:
https://forums.developer.apple.com/forums/thread/115405

Issues Fixed

Fixes #25946

Before After
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2024-11-27.at.12.54.42.mp4
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2024-11-27.at.12.52.55.mp4

@kubaflo kubaflo requested a review from a team as a code owner November 27, 2024 12:20
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Nov 27, 2024
@kubaflo kubaflo added area-controls-collectionview CollectionView, CarouselView, IndicatorView area-controls-label Label, Span community ✨ Community Contribution and removed community ✨ Community Contribution labels Nov 27, 2024
@rmarinho

This comment was marked as outdated.

This comment was marked as outdated.

@kubaflo
Copy link
Contributor Author

kubaflo commented Nov 27, 2024

I now noticed that the crash happens with CarouselView1 only when swiping between more than 2 items, but in the case of CarouselView2 the app crashes instantly when CarouselView2 has more than 1 item

<CarouselView Grid.Row="1">
    <CarouselView.ItemsSource>
        <x:Array Type="{x:Type x:String}">
            <x:String>Item1</x:String>
            <x:String>Item2</x:String>
        </x:Array>
    </CarouselView.ItemsSource>
    <CarouselView.ItemTemplate>
        <DataTemplate>
            <VerticalStackLayout Background="Red">
                <Label Text="Hello"/>
                <Label Text="{Binding .}"
                        TextType="Html"/>
            </VerticalStackLayout>
        </DataTemplate>
    </CarouselView.ItemTemplate>
</CarouselView>

@kubaflo
Copy link
Contributor Author

kubaflo commented Nov 27, 2024

Ohh I just bumped into a scaling issue with ListView/CollectionView1 and CollectionView1 after changes in this PR:

For the following code:

<Grid RowDefinitions="*,*">
    <CollectionView>
        <CollectionView.ItemsSource>
            <x:Array Type="{x:Type x:String}">
                <x:String>Item1</x:String>
                <x:String>Item1</x:String>
                <x:String>Item1</x:String>
            </x:Array>
        </CollectionView.ItemsSource>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <VerticalStackLayout>
                    <Label Text="Hello"/>
                    <Label Text="{Binding .}"
                            TextType="Html"/>
                </VerticalStackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

    <StackLayout Grid.Row="1">
        <BindableLayout.ItemsSource>
            <x:Array Type="{x:Type x:String}">
                <x:String>Item1</x:String>
                <x:String>Item1</x:String>
                <x:String>Item1</x:String>
            </x:Array>
        </BindableLayout.ItemsSource>
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <VerticalStackLayout>
                    <Label Text="Hello"/>
                    <Label Text="{Binding .}"
                            TextType="Html"/>
                </VerticalStackLayout>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
</Grid>

I have a cell scaling issue

It gets automatically fixed after updating any of the Label's properties
@rmarinho do you maybe have an idea why it happens?

Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nop, not from the top of my mind, can we rebase, see if it's still a issue and also add a test case for this ?

@kubaflo
Copy link
Contributor Author

kubaflo commented Dec 10, 2024

Nop, not from the top of my mind, can we rebase, see if it's still a issue and also add a test case for this ?

I've rebased, but the issue is still there without using MainQueue.DispatchAsync
However, I'm not sure if it is a correct fix, because it still regresses the height of HTML text in listView and cv

@rmarinho

This comment was marked as outdated.

This comment was marked as outdated.

rmarinho
rmarinho previously approved these changes Dec 13, 2024
Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing tests on iOS not related

@rmarinho rmarinho added this to the .NET 9 SR3 milestone Dec 13, 2024
@samhouts samhouts requested a review from Copilot December 13, 2024 21:41
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

@samhouts samhouts requested a review from Copilot December 13, 2024 21:50
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

src/Core/src/Platform/iOS/LabelExtensions.cs:93

  • The NSError variable nsError is not checked after the NSAttributedString initialization. Add a check for nsError and handle any errors appropriately.
platformLabel.AttributedText = new NSAttributedString(text, attr, ref nsError);

@samhouts samhouts requested a review from Copilot December 13, 2024 21:51
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

src/Core/src/Platform/iOS/LabelExtensions.cs:93

  • Check and handle nsError after assigning NSAttributedString to ensure any errors are properly managed.
platformLabel.AttributedText = new NSAttributedString(text, attr, ref nsError);

@kubaflo
Copy link
Contributor Author

kubaflo commented Dec 17, 2024

Item1 Item1 Item1
<StackLayout Grid.Row="1">
    <BindableLayout.ItemsSource>
        <x:Array Type="{x:Type x:String}">
            <x:String>Item1</x:String>
            <x:String>Item1</x:String>
            <x:String>Item1</x:String>
        </x:Array>
    </BindableLayout.ItemsSource>
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <VerticalStackLayout>
                <Label Text="Hello"/>
                <Label Text="{Binding .}"
                        TextType="Html"/>
            </VerticalStackLayout>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

I tested and this is fixed with this PR: #25664 But, I believe @albyrock87 divided it into smaller ones

@jfversluis
Copy link
Member

jfversluis commented Dec 18, 2024

/azp run

This comment was marked as off-topic.

@mattleibow mattleibow changed the base branch from main to inflight/current May 30, 2025 21:57
@mattleibow mattleibow merged commit 231ff0a into dotnet:inflight/current May 30, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from Approved to Done in MAUI SDK Ongoing May 30, 2025
github-actions bot pushed a commit that referenced this pull request Jun 2, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 6, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 6, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 7, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 10, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 10, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 11, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 11, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 16, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 16, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
github-actions bot pushed a commit that referenced this pull request Jun 19, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
PureWeen added a commit that referenced this pull request Jun 21, 2025
For more information about inflight process check
https://github.com/dotnet/maui/wiki/Inflight-Branch-Process

# .NET MAUI Release Notes - inflight/candidate Branch

## MAUI Product Fixes
* [iOS] Fix SwipeView programmatic open when background color is set by
@Shalini-Ashokan in #29765
* [iOS] FormattedText with text color causes stack overflow - fix by
@kubaflo in #29874
* [iOS] Fix AdaptiveTrigger not working as expected by @jsuarezruiz in
#20987
* [iOS] Fix for Flyout title is not broken over multiple lines when you
rotate your screen by @BagavathiPerumal in
#29171
* [Windows] Fix for Assigning null to the SelectedItem of the
CollectionView in the SelectionChanged event does not clear the
selection by @SyedAbdulAzeemSF4852 in
#29288
* Fix TapCoordinates method in Catalyst by @jsuarezruiz in
#29775
* Fixed ItemSpacing on CarouselView resizes items [Android] by
@Dhivya-SF4094 in #29796
* Perf : ButtonContentTypeConverter now using InvariantCulture to parse
double by @antoine-jonathan in #25393
* ToolbarItem behavior with ImageSource iOS - fix by @kubaflo in
#28833
* [Android] Fixed Button Shadow Color Transparency Not Applied Correctly
by @NanthiniMahalingam in #29371
* Don't call NSAttributedString with HTML from a background thread by
@kubaflo in #26153
* [Windows] Implemented the Resize and Downsize functions in the
W2DImage class by @HarishwaranVijayakumar in
#29138
* Templated indicator view - improvements by @kubaflo in
#25642
* Update Issue25946.xaml by @anandhan-rajagopal in
#30025

## Testing
* Fixed Test case failure in PR 26153 - iOS by @kubaflo in
#29906
* Fixed Test case failure in PR 25642 - snapshots by @kubaflo in
#29881
* [Testing] Feature Matrix UITest Cases for RadioButton by
@TamilarasanSF4853 in #29744
* [Testing] Feature Matrix UITest Cases for CollectionView ItemsSource
Feature by @LogishaSelvarajSF4525 in
#29322
* [Testing] Feature Matrix UITest Cases for Stepper by
@nivetha-nagalingam in #29731
* [Testing] Feature Matrix UITest Cases for CheckBox Control by
@anandhan-rajagopal in #29739
* Update the comment for the test properties by @mattleibow in
#27306

## Dependency Updates
* No changes in this category

## Docs
* No changes in this category

## Housekeeping
* [create-pull-request] automated change by @github-actions[bot] in
#29875
* [create-pull-request] automated change by @github-actions[bot] in
#29792
* [create-pull-request] automated change by @github-actions[bot] in
#29760

**Full Changelog**:
origin/main...origin/inflight/candidate
@PureWeen PureWeen modified the milestones: .NET 9 SR8, .NET 9 SR9 Jun 23, 2025
rmarinho pushed a commit that referenced this pull request Jun 24, 2025
)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
NanthiniMahalingam pushed a commit to NanthiniMahalingam/maui that referenced this pull request Jun 27, 2025
…net#26153)

* Don’t call NSAttributedString with HTML from a background thread

* Added a UITest

* Refactor

* add more comments and move to Controls

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
NanthiniMahalingam added a commit to NanthiniMahalingam/maui that referenced this pull request Jul 1, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jul 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution partner/syncfusion/review
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

iOS App crashes on iOS 18 when placing html label in carousel view with > 2 elements
6 participants