I’m writing an NSOperation subclass. Instances are dumped into the main queue, 
but I hope I stop them from executing right away by overriding -isReady. But 
the docs say I have to keep the override KVO-compliant. And my formula for the 
new -isReady uses the old one. Can I observe on super? Without causing infinite 
recursion if I override an attribute I’m watching? Here’s my header:

> @interface PrBulkFileOperation : NSOperation
> 
> +(instancetype)openFiles:(NSArray *)paths application:(NSApplication *)app;
> 
> @property (readonly) NSArray *        files;  // Elements are NSURL*
> @property (readonly) NSApplication *  application;
> 
> @end


And the applicable source:

> @interface PrBulkFileOperation () {
>     NSArray *      _files;
>     NSApplication *  _app;
> 
>     NSMutableSet  *_cancelledFiles, *_failedFiles, *_successfulFiles;
> 
>     NSMutableDictionary *  _fileFromBrowser;
> }
> 
> @property (readonly) NSSet *cancelledFiles, *failedFiles, *successfulFiles;
> @property (readonly) NSMutableSet *mutableCancelledFiles, 
> *mutableFailedFiles, *mutableSuccessfulFiles;
> 
> @property NSMutableDictionary *  fileFromBrowser;
> 
> - (instancetype)initWithFiles:(NSArray *)paths application:(NSApplication 
> *)app;
> 
> @end
> 
> @implementation PrBulkFileOperation
> 
> #pragma mark Property getters & setters
> 
> @synthesize files;
> 
> @synthesize application = _app;
> 
> @synthesize cancelledFiles  = _cancelledFiles;
> @synthesize failedFiles     = _failedFiles;
> @synthesize successfulFiles = _successfulFiles;
> @synthesize fileFromBrowser = _fileFromBrowser;
> 
> - (void)addCancelledFilesObject:(NSURL *)file {
>     [_cancelledFiles addObject:file];
> }
> 
> - (void)removeCancelledFilesObject:(NSURL *)file {
>     [_cancelledFiles removeObject:file];
> }
> 
> - (NSMutableSet *)mutableCancelledFiles {
>     return [self mutableSetValueForKey:@"cancelledFiles"];
> }
> 
> - (void)addFailedFilesObject:(NSURL *)file {
>     [_failedFiles addObject:file];
> }
> 
> - (void)removeFailedFilesObject:(NSURL *)file {
>     [_failedFiles removeObject:file];
> }
> 
> - (NSMutableSet *)mutableFailedFiles {
>     return [self mutableSetValueForKey:@"failedFiles"];
> }
> 
> - (void)addSuccessfulFilesObject:(NSURL *)file {
>     [_successfulFiles addObject:file];
> }
> 
> - (void)removeSuccessfulFilesObject:(NSURL *)file {
>     [_successfulFiles removeObject:file];
> }
> 
> - (NSMutableSet *)mutableSuccessfulFiles {
>     return [self mutableSetValueForKey:@"successfulFiles"];
> }
> 
> #pragma mark KVO management
> 
> + (NSSet *)keyPathsForValuesAffectingIsReady {
>     return [NSSet setWithObjects:@"super.isReady", @"cancelledFiles", 
> @"failedFiles", @"successfulFiles", nil];
> }
> 
> #pragma mark Conventional overrides
> 
> - (BOOL)isReady {
>     return [super isReady] && (self.cancelledFiles.count + 
> self.failedFiles.count + self.successfulFiles.count >= files.count);
> }
> 
> @end

Are my key paths for my own attributes correct? Can I point to an attribute 
from super with “super.whatever”? Can I really trigger my ready flag with just 
indirect observations on the four attributes I need?

— 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com 

_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to