SizeWithFont in MultiThread Crash!

sizeWithFont crashed into multithreading, this is debugging information:

1 0x00a0df8e in icu::RuleBasedBreakIterator::handleNext
2 0x00a0daff in icu::RuleBasedBreakIterator::next
3 0x00a0d174 in icu::RuleBasedBreakIterator::following
4 0x35879719 in WebCore::nextBreakablePosition
5 0x3587842a in -[NSString(WebStringDrawing) _web_drawInRect:withFont:ellipsis:alignment:lineSpacing:includeEmoji:measureOnly:]
6 0x35877da3 in -[NSString(WebStringDrawing) _web_sizeInRect:withFont:ellipsis:lineSpacing:]
7 0x3090d238 in -[NSString(UIStringDrawing) sizeWithFont:constrainedToSize:lineBreakMode:lineSpacing:]
8 0x3090cee3 in -[NSString(UIStringDrawing) sizeWithFont:constrainedToSize:lineBreakMode:]

Now I solve the error using the NSLock object, before using this function, I will lock this object and after that unlock it

but I think there should be a better solution!

and I found that this error only appeared when the NSString object for this function on both two threads is multi-line text

+3
source share
3 answers

Generally, you should not call UIKit [1] methods from a separate thread. It doesn't matter if you use locks, this is not a starter.

, , , UIKit, . performSelectorOnMainThread: withObject: waitUntilDone: :

http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject:waitUntilDone:

MonoTouch: foo.InvokeOnMainThread(delegate {your_code_here});

[1] iOS 4.0 API.

+4
  • , .

  • "" . . .

  • "" , , , .

  • . , . .

, , , . , . , .

+4

, performSelectorOnMainThread: withObject: waitUntilDone: ,

, waitUntilAllOperationsAreFinished , ,
performSelectorOnMainThread: withObject: waitUntilDone waitUntilDone ( )

, waitUntilAllOperationsAreFinished ,

                    [md removeAllObjects];
                    [md setObject:subString forKey:@"text"];
                    [md setObject:[NSNumber numberWithInt:view_w ] forKey:@"width"];
                    [md setObject:[NSNumber numberWithInt:height_left + font_h ] forKey:@"height"];
                    [self  performSelectorOnMainThread:
                     @selector(calculateTextRegion:)
                                            withObject:md
                                         waitUntilDone:YES];
                    CGSize stringSize = textRegion;
0

Source: https://habr.com/ru/post/1760668/


All Articles