I could not find the answer anywhere on the net, so any help would be appreciated.
I will contact to create a system in which I can get the results of the NSOperation task, which, as I understand it, cannot be performed using specific subclasses such as NSInvocation.
I have a subclass of NSOperation ( TheEngine ), which is abstract by convention and needs to be extended to implement the -main function to include the body of the executable code.
TheEngine contains the following initialization function, the task of which is simply to mark theSelector and theObject to which the selector belongs. It also registers the KV observer for the isFinished property:
-(id)initWithCallbackSelector:(SEL)theSelector inObject:(id)theObject
In my function observeValueForKeyPath:ofObject:change:context: I would like to call the callback function as follows:
NSLog(@"Some debug text to ensure this function is being called", nil); [theObject performSelector:theSelector withObject:someData afterDelay:0];
The whole process is as follows:
aViewController launches TheEngine extension - allows you to tell TheTask by invoking the following and adding it to the operation queue.
TheTask* TT = [[TheTask alloc] initWithCallbackSelector: @selector(resultHandler:) inObject:theObject];
Everything seems to work as expected without any errors or exceptions. But when the execution reaches observeValueForKeyPath:ofObject:change:context: callback is not actually called. I am new to Obj-C, so I'm not quite sure if I understand this type of thread correctly.
Here is the whole code:
-(id)initWithCallbackSelector:(SEL)theSelector inObject:(id)theObject{ if([self init]){ self.selectorsParentObject = theObject; self.selectorToCallWhenFinished = theSelector; [self addObserver:self forKeyPath:@"isFinished" options:NSKeyValueObservingOptionNew context:NULL]; return self; } return nil; } -(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)theObject change:(NSDictionary*)theChange context:(void*)theContext{ if([keyPath isEqualToString:@"isFinished"]){ NSLog(@"activity is finished with change: %@", theChange); NSLog(@"target object: %@", self.selectorsParentObject); NSLog(@"target selector: %@", NSStringFromSelector(self.selectorToCallWhenFinished));
Any help appreciated!