> So my potential solution for this is:
> 
> NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
> __block NSRange range = NSMakeRange(0, 0);
> while (range.location < [aString length]) {
> 
>    dispatch_sync(dispatch_get_main_queue(), ^{
>      range = [checker checkSpellingOfString:aString 
> startingAt:range.location];
>    });
> 
>   // update range
> }
> 
> Where this piece of code will run on multiple threads.
> 
> Does this look like a reasonable approach to ensuring thread safety?

In addition to the potential for deadlocking that Mike warned about, it's 
probably best to be paranoid about +sharedSpellChecker. Hopefully that method 
uses a thread-safe singleton allocation (eg: via dispatch_once), but you never 
know. I'd move that method call to the main thread too.

However, by using this approach you're not really doing the spell checking work 
on a background thread. The work is going to be done synchronously on the main 
thread, blocking your GUI (and whatever else). Maybe you should investigate 
using -requestCheckingOfString:etc: instead.

~Martin



_______________________________________________

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