From 8952fce1ea8c71ab5065ff609d936d54634e91f2 Mon Sep 17 00:00:00 2001 From: Galvin Li Date: Wed, 5 Oct 2016 11:11:08 +0800 Subject: [PATCH] add an NSOperationQueue to read the outputPipe, low the CPU usage remove the KVO for availablePorts, because it never handle and may crash the app --- Sources/HexLoaderController.m | 53 ++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/Sources/HexLoaderController.m b/Sources/HexLoaderController.m index 1406996..3644442 100644 --- a/Sources/HexLoaderController.m +++ b/Sources/HexLoaderController.m @@ -1,6 +1,12 @@ #import "HexLoaderController.h" #import "ORSSerialPortManager.h" +@interface HexLoaderController () + +@property (nonatomic, strong) NSOperationQueue *bgQueue; + +@end + @implementation HexLoaderController @synthesize sendTextField = _sendTextField; @@ -17,6 +23,10 @@ - (id)init self = [super init]; if (self) { + self.bgQueue = [[NSOperationQueue alloc] init]; + if ([self.bgQueue respondsToSelector:@selector(setQualityOfService:)]) { + [self.bgQueue setQualityOfService:NSQualityOfServiceBackground]; + } self.serialPortManager = [ORSSerialPortManager sharedSerialPortManager]; self.availableBaudRates = [NSArray arrayWithObjects: [NSNumber numberWithInteger:300], @@ -195,20 +205,30 @@ - (IBAction)sendFileButtonAction:(id)sender{ [[outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify]; - [[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification object:[outputPipe fileHandleForReading] queue:nil usingBlock:^(NSNotification *notification){ - + [[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification object:[outputPipe fileHandleForReading] queue:self.bgQueue usingBlock:^(NSNotification *notification){ + + NSData *output = [[outputPipe fileHandleForReading] availableData]; NSString *outStr = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding]; - [self.receivedDataTextView.textStorage.mutableString appendString:[NSString stringWithFormat:@"%@", outStr]]; + if ([outStr isEqualToString:@""]) { + [[outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify]; + return ; + } + + dispatch_async(dispatch_get_main_queue(), ^{ + [self.receivedDataTextView.textStorage.mutableString appendString:[NSString stringWithFormat:@"%@", outStr]]; + + // Scroll to end of outputText field + NSRange range; + range = NSMakeRange([self.receivedDataTextView.string length], 0); + [self.receivedDataTextView setFont:[NSFont fontWithName:@"Monaco" size:10]]; + + [self.receivedDataTextView scrollRangeToVisible:range]; + [self.receivedDataTextView setNeedsDisplay:YES]; + [[outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify]; + }); + - // Scroll to end of outputText field - NSRange range; - range = NSMakeRange([self.receivedDataTextView.string length], 0); - [self.receivedDataTextView setFont:[NSFont fontWithName:@"Monaco" size:10]]; - - [self.receivedDataTextView scrollRangeToVisible:range]; - [self.receivedDataTextView setNeedsDisplay:YES]; - [[outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify]; }]; NSLog(@""); @@ -231,17 +251,6 @@ - (IBAction)sendFileButtonAction:(id)sender{ } } -- (void)setSerialPortManager:(ORSSerialPortManager *)manager -{ - if (manager != _serialPortManager) - { - [_serialPortManager removeObserver:self forKeyPath:@"availablePorts"]; - _serialPortManager = manager; - NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld; - [_serialPortManager addObserver:self forKeyPath:@"availablePorts" options:options context:NULL]; - } -} - - (void)serialPort:(ORSSerialPort *)serialPort didReceiveData:(NSData *)data { NSLog(@"%@", data); }