Aaron Vegh
Aaron Vegh

Reputation: 5217

NSInvocationOperation init returns nil calling instance method from a class method

I'm trying to drop NSOperation objects into an NSOperationQueue. But it seems I'm misunderstanding how to properly initialize an NSInvocationOperation (a subclass that seems tailor-made for my purposes, as I want to execute an existing method inside the operation queue). Here's what I'm trying:

OnlineServiceManager * sm = [[OnlineServiceManager alloc] initWithAsset:asset andViewController:viewController];
NSInvocationOperation * operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(pushAsset) object:sm];
[queue addOperation:operation];

This stanza is operating within a loop that provides the new asset to be pushed in the operation. As I debug these lines, it turns out that operation is nil, suggesting that the selector can't be found. The selector is an instance method in this class, with this signature:

- (void)pushAsset

That stanza is also operating in a class method — my intention is to call the class and instruct it to push all assets.

Having searched all the docs I can find relevant to this case, I don't see quite what I'm doing wrong.

Upvotes: 2

Views: 362

Answers (2)

Bored Astronaut
Bored Astronaut

Reputation: 994

You cannot invoke an instance method on a class object. If you want -pushAsset to continue to be an instance method, then you must give the NSOperation an instance to invoke it on (and that instance must still be around when the operation runs). What object do you want to perform the -pushAsset operation? That's what should be used as the Target argument.

Upvotes: 1

griotspeak
griotspeak

Reputation: 13257

self is the class object and push asset is an instance method, which MIGHT be the issue. What happens if you change push asset to a class method?

Upvotes: 1

Related Questions