我正在尝试从javascript中使用的回调方法调用委托。但是当我从回调中执行委托时,我收到错误"Can only be called from main thread"
,而不是在事件触发时。
注意:错误出现在最后一行代码中。
这是代码的样子
RCT_EXPORT_METHOD(setBold) {
if(_bold == NO)
{
_bold = YES;
}
else if (_bold == YES)
{
_bold = NO;
}
else{
_bold = NO;
}
if(_textV!=nil)
{
_delCalled = YES;
[self textViewDidChangeSelection:_textV];
}
}
-(void)textViewDidChangeSelection:(UITextView *)textView
{
_textV = textView;
_selectedText = [textView.text substringWithRange:textView.selectedRange];
_rangeStart = textView.selectedRange.location;
_rangeEnd = textView.selectedRange.length;
if(_delCalled == YES)
{
[self applyBold:textView];
_delCalled = NO;
}
NSDictionary *event = @{
@"target": textView.reactTag,
@"highlights": @{
@"text": textView.text,
@"range": @(textView.selectedRange.length),
@"cursorPosition": @(textView.selectedRange.location),
@"eventType": @"textViewDidChangeSelection",
}
};
[self.bridge.eventDispatcher sendInputEventWithName:@"topChange" body:event];
}
-(void)applyBold:(UITextView *)textView
{
NSMutableAttributedString *formatString;
UIFont* boldFont;
NSString *selectedText = _selectedText;
_rangeStart -=1;
if(_rangeEnd == 0)
{
_rangeEnd = 1;
}
else
{
_rangeStart +=1;
}
NSRange range = NSMakeRange(_rangeStart, _rangeEnd);
if (_bold==YES) {
boldFont= [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
}
else{
boldFont= [UIFont systemFontOfSize:textView.font.pointSize];
}
NSAttributedString *lem = textView.attributedText;
NSMutableAttributedString *textViewText = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];
NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];
formatString = [[NSMutableAttributedString alloc]initWithString:selectedText attributes:boldAttr];
if(![selectedText isEqual:@""] || ![selectedText isEqual:@" "])
{
[textViewText replaceCharactersInRange:range withAttributedString:formatString];
//this is where the error happens
textView.attributedText = textViewText;
}
}
答案 0 :(得分:2)
在iOS中,必须在主线程上调用更新UI的任何方法。 iOS UIKit不是线程安全的。所以,解决方案是将代码放在主线程上,就像这个
dispatch_async( dispatch_get_main_queue(), ^{
textView.attributedText = textViewText;
});
答案 1 :(得分:1)
将带有主队列的dispatch_async中生成错误的行包含在内
dispatch_async(dispatch_get_main_queue(), ^{
...
});