NSOperation - Update UI with delegate or in a Controller with oberseValueForKeyPath:
NSOperation - Update UI with delegate or in a Controller with oberseValueForKeyPath:
- Subject: NSOperation - Update UI with delegate or in a Controller with oberseValueForKeyPath:
- From: Gilles Celli <email@hidden>
- Date: Thu, 24 Apr 2014 15:47:56 +0200
Hello Cocoa-Meisters,
My Document-based OS X program is parsing huge ASCII data files in an NSOperationQueue, and then displays the data in a graph.
It works, however I've seen 2 different approaches to update the User-Interface (UI) after the NSOperation has finished (please check code below):
1. With KVO in a Controller object: Observing the NSOperation's "isFinished" value and then in observeValueForKeyPath:ofObject:change:context:
update the UI (graph display) on the main thread with performSelectorOnMainThread:withObject:waitUntilDone
2. No KVO: in NSOperation the UI is updated after the parsing is done, by calling the delegate again with
performSelectorOnMainThread:withObject:waitUntilDone
I've tested both approaches but can't decide which one is the most correct / best, or what are the benefits of using one approach or another ?
Any suggestions or help is greatly appreciated..
Regards,
Gilles
Some code snippets here:
//-----------------------------------------------
// 1. With KVO in a Controller object
//-----------------------------------------------
MyController.m
...
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
....
// Update the User-Interface and graph view
if ( [keyPath isEqual:@"isFinished"] && asciiParserOperation == object )
{ ...
[self performSelectorOnMainThread:@selector(operationAsciiParsingDone:) withObject:[asciiParserOperation parsedFile] waitUntilDone:NO];
}
....
}
//-----------------------------------------------
// 2. NSOperation with delegate
//-----------------------------------------------
-(void)main
{
@autoreleasepool {
// Do the parsing here
parsedAscii = [[AsciiParser alloc] initWithContentsOfString:myAsciiString]
// Update the User-Interface and graph view via delegate
if( self.delegate && [self.delegate respondsToSelector:@selector(operationAsciiParsingDone:)] )
{
[(NSObject*)delegate performSelectorOnMainThread:@selector(operationAsciiParsingDone:)
withObject:[self parsedAscii] waitUntilDone:NO];
}
}
.....
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden