On May 8, 2013, at 3:14 PM, Jens Alfke <j...@mooseyard.com> wrote:
> On May 7, 2013, at 3:52 PM, Ken Thomases <k...@codeweavers.com> wrote:
> 
>> Yes, it can.  Under the hood, NSAttributedString is using WebKit for HTML 
>> rendering.  In part that means that, when invoked from a background thread, 
>> it has to shunt the work to the main thread.  But it also means the main 
>> thread may have to run the run loop during the call.  It's a nuisance, but 
>> it's necessary since HTML can have references to external resources that 
>> need to be loaded.
> 
> I’ve had trouble with this method in the past, for exactly that reason — you 
> can get weird reentrancy problems from runloop sources like timers being 
> invoked while in the middle of the call. (It’s also pretty slow.)
> 
> IMHO it’s best to avoid this method if you can. For example, the last time 
> this came up all I needed was the plain text, so I wrote a little string 
> transformer to strip out HTML tags and expand HTML entities. For more 
> involved work you could use NSXMLParser (with the “tidy” option) to parse the 
> HTML into a DOM and then walk through that.

Yup. I had edge-case crashes too (fortunately reproducible one I knew the right 
edge case), and spent hours tracking it down to reentrancy problems in 
initWithHTML. Fortunately I could count on getting well-formed XML, and like 
Jens all I needed was to extract plain text, so I changed my solution to use 
NSXMLDocument and the crash went away.

Here's the link to the NSAttributedString class reference. I just now went to 
the bottom of the page and submitted feedback requesting that the docs warn 
about this pitfall. Perhaps others could do the same, on the chance that it 
will help some future programmer track down the problem a little sooner.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSAttributedString_AppKitAdditions/Reference/Reference.html

I referenced this email thread in my comments:

http://lists.apple.com/archives/cocoa-dev/2013/May/msg00117.html

--Andy


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to