2

I am trying to call a class method in my application: didFinishLaunchingWithOptions: using NSThread. But strangely this method is not getting called. I have tried the below in didFinishLaunchingWithOptions method.

[self performSelector:@selector(parseAdContent) onThread:myThread withObject:AdvertisementView waitUntilDone:NO];
      [myThread start];

help me in this.

2 Answers2

0

This is how you create a separate thread:

[NSThread detachNewThreadSelector:@selector(yourSelector:) toTarget:yourTarget withObject:objectYouWishToPassAsParameter];

The selector you specify will automatically get executed on a new thread.

Particular example for your code:

[NSThread detachNewThreadSelector:@selector(parseAdContent:) toTarget:self withObject:AdvertisementView];

And your thread selector should look like this:

- (void)parseAdContent:(id)obj {
    NSAutoreleasePool *threadPool = [[NSAutoreleasePool alloc] init];
    // Your background thread code here

    [pool drain];
}
Vlad
  • 3,346
  • 2
  • 27
  • 39
  • OP said that he's trying to call a class method. Shouldn't target be [self class]? – tronbabylove May 10 '12 at 14:34
  • @tronbabylove Thanks for your reply, but I am not getting wat is expected. Target cant be self or self class, why because I am having the method in another class called Adview where AdvertisementView is the instance variable of that class in applicationDelagate class. anymore help please – user1188275 May 11 '12 at 06:43
  • @user1188275 It's located in the Adview class - so is it a class method or an instance method? – tronbabylove May 11 '12 at 13:46
  • for the method example I used above (- (void)parseAdContent:(id)obj), target is self, not [self class]. If you were to use a static method (ex: + (id)someMethod:(id)param) target would indeed be [self class]. My example should have been descriptive enough] – Vlad May 14 '12 at 07:41
0

You want to call a class method, but this call is trying to invoking self's -parseAdContent:. I don't know what class this is being called in, but either way, the target here is the instance variable self and not a class. Something like this is probably more what you are looking for.

[myThread initWithTarget:[self class] selector:@selector(parseAdContent) object:AdvertisementView];

EDIT: So if the method is a class method in the Adview class, just change the target to the Adview class...

[myThread initWithTarget:[Adview class] selector:@selector(parseAdContent) object:AdvertisementView];

But if it is an instance method in the Adview class, then the target will be the instance of variable of type Adview - AdvertisementView, right?

[myThread initWithTarget:AdvertisementView selector:@selector(parseAdContent) object:AdvertisementView];

But this wouldn't make sense, you wouldn't call an instance method and pass the instance as a parameter to its own method... The target is the object for which you are invoking the selector. The object: parameter is used the selector's argument.

I'm assuming that this is a class method as you stated originally, in which case the first of these two calls should work.

tronbabylove
  • 1,102
  • 8
  • 12
  • I tried both of your example, making my method as class and instance method but the execution cycle does not pass through the method. I am totaly lost. fixed with this for the past two days !!. Thanks for ur reply. – user1188275 May 12 '12 at 10:37
  • @user1188275 Another thing I noticed is that you are passing a parameter to the method (AdvertisementView), but you did not include the ':' in your selector tag. It should be "selector:(parseAdContent:)" I think adding the colon makes it a completely different selector - try adding it and see if it changes anything. – tronbabylove May 12 '12 at 14:33
  • @user1188275 One more note - according to the Apple Docs, "the selector must take only one argument and must not have a return value." So make sure that parseAdContent is not returning anything. – tronbabylove May 12 '12 at 14:41