How can an instance KVO its own and super's attributes, especially with overrides?
How can an instance KVO its own and super's attributes, especially with overrides?
- Subject: How can an instance KVO its own and super's attributes, especially with overrides?
- From: Daryle Walker <email@hidden>
- Date: Thu, 21 Aug 2014 00:02:28 -0400
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@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