ayl
ayl

Reputation: 404

main() not called in non concurrent NSOperation

When creating an NSOperation and putting it into an NSOperationQueue, I never see main() being called. Only start() is getting called. I'm not doing anything fancy, really. As a simple test, I wrote this:

NSOperationQueue *testOperationQueue = [[NSOperationQueue alloc] init];
MyTestOperation *testOperation = [[MyTestOperation alloc] init];
[testOperationQueue addOperation:testOperation];

in MyTestOperation.m:

- (void)main
{
  NSLog(@"testing if main is getting called");
}

- (void)start
{
  NSLog(@"testing if start is getting called");
}

MyTestOperation.h looks like this:

#import <Foundation/Foundation.h>

@interface MyTestOperation : NSOperation

@end

Am I missing something obvious?

[Edit Note: I actually meant non concurrent, not concurrent (as written in the previous title).]

Upvotes: 2

Views: 1899

Answers (1)

Sean
Sean

Reputation: 5820

I played around with your code and noticed that if I commented out your start method entirely, the main method would run. After reading the documentation for start more closely, I found this line:

The default implementation of this method updates the execution state of the operation and calls the receiver’s main method.

Thus, by overriding start with an implementation that didn't call main, you effectively short-circuited the operation and never allowed it to actually begin working! Therefore, you either need to explicitly call main within your start method, or you need to eliminate start all-together if you're not actually using it to do setup/update execution state/etc.

Upvotes: 7

Related Questions