I am running another processing thread, and I would like to log results to a NSTextView, that when a new line is posted, it updates the view and positions the scrollbar to the bottom.
Any suggestions?
- (void)runProc
{
do {
[NSThread sleepForTimeInterval:0.1];
[self reportInfo:@"tick"];
} while (stop == NO);
}
- (void)report:(NSString*)string;
{
[[consoleView textStorage] beginEditing];
[[[consoleView textStorage] mutableString] appendString:string];
[[[consoleView textStorage] mutableString] appendString:@"\n"];
[[consoleView textStorage] endEditing];
NSRange range;
range = NSMakeRange ([[consoleView string] length], 0);
[consoleView scrollRangeToVisible: range];
}
It gets to about 50 entries then locks the whole thing up.
Most of AppKit is not thread safe, you can’t update an
NSTextViewfrom a secondary thread. All UI updates must be done on the main thread.You need to call your reporting method like this: