-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Don’t call NSAttributedString with HTML from a background thread #26153
base: main
Are you sure you want to change the base?
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
I now noticed that the crash happens with
|
Ohh I just bumped into a scaling issue with ListView/CollectionView1 and CollectionView1 after changes in this PR: For the following code:
It gets automatically fixed after updating any of the Label's properties |
There was a problem hiding this 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 ?
I've rebased, but the issue is still there without using |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this 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
#pragma warning disable CS8601 | ||
platformLabel.AttributedText = new NSAttributedString(text, attr, ref nsError); | ||
#pragma warning restore CS8601 | ||
CoreFoundation.DispatchQueue.MainQueue.DispatchAsync(() => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might not be following your description quite right but should we check if we're on a background thread before dispatching?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With something like this:
NSError nsError = new();
if (NSThread.IsMain)
{
platformLabel.AttributedText = new NSAttributedString(text, attr, ref nsError);
if (label.Handler is ILabelHandler labelHandler)
{
labelHandler.UpdateValue(nameof(ILabel.TextColor));
labelHandler.UpdateValue(nameof(ILabel.Font));
}
label.InvalidateMeasure();
}
else
{
CoreFoundation.DispatchQueue.MainQueue.DispatchAsync(() =>
{
platformLabel.AttributedText = new NSAttributedString(text, attr, ref nsError);
if (label.Handler is ILabelHandler labelHandler)
{
labelHandler.UpdateValue(nameof(ILabel.TextColor));
labelHandler.UpdateValue(nameof(ILabel.Font));
}
label.InvalidateMeasure();
});
}
The list view/collection view with the HTML label looks right, but the app crashes when using the collection view
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use a local function to avoid code duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will the async cause issues because that would be unexpected? Will DispatchSync work/have issues/locking?
There was a problem hiding this 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.
There was a problem hiding this 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);
There was a problem hiding this 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);
Item1 Item1 Item1 I tested and this is fixed with this PR: #25664 But, I believe @albyrock87 divided it into smaller ones |
/azp run |
This comment was marked as off-topic.
This comment was marked as off-topic.
/rebase |
0b99f74
to
c102604
Compare
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comment, but I just want to check. How do we know this is on a background thread? All UIKit operations are on a main thread - in fact, all of our handlers are on a main thread.
Something has gone horribly wrong if a handler mapper is updating the UI from a background thread. Where did it switch and why?
I assume this only happens in CV, so this may be some weird case where the CV is moving to a background thread... to update the UI? Feels very sus.
#pragma warning disable CS8601 | ||
platformLabel.AttributedText = new NSAttributedString(text, attr, ref nsError); | ||
#pragma warning restore CS8601 | ||
CoreFoundation.DispatchQueue.MainQueue.DispatchAsync(() => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use a local function to avoid code duplication.
@@ -38,13 +38,6 @@ public static void MapMaxLines(ILabelHandler handler, Label label) | |||
|
|||
static void MapFormatting(ILabelHandler handler, Label label) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like I wrote this code and overengineered this. It appears there is just 1 usage of the MapFormatting method, and it was already weird. Maybe this should just be merged with the MapText or UpdateText?
#pragma warning disable CS8601 | ||
platformLabel.AttributedText = new NSAttributedString(text, attr, ref nsError); | ||
#pragma warning restore CS8601 | ||
CoreFoundation.DispatchQueue.MainQueue.DispatchAsync(() => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will the async cause issues because that would be unexpected? Will DispatchSync work/have issues/locking?
if (label.Handler is ILabelHandler labelHandler) | ||
{ | ||
labelHandler.UpdateValue(nameof(ILabel.TextColor)); | ||
labelHandler.UpdateValue(nameof(ILabel.Font)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if using handlers here is expected... Not sure if that is bad or good, but in my mind all the handler things happen in the handler or close to it. This is a few methods away now so may be surprising.
@kubaflo some comments! |
@mattleibow I've refactored a bit |
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
According to Apple's documentation: https://developer.apple.com/documentation/foundation/nsattributedstring/1524613-initwithdata
Also, it seems to be a common problem among Apple developers:
https://forums.developer.apple.com/forums/thread/115405
Issues Fixed
Fixes #25946
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