开发者

iphone: performSelector: withObject: afterDelay: does not work with a background thread?

开发者 https://www.devze.com 2023-01-14 03:10 出处:网络
I want to run a meth开发者_开发问答od in a background thread, the first method will run another method on the same (background) thread after some seconds. I wrote this:

I want to run a meth开发者_开发问答od in a background thread, the first method will run another method on the same (background) thread after some seconds. I wrote this:

- (IBAction)lauch:(id)sender
{
    [self performSelectorInBackground:@selector(first) withObject:nil];

}
-(void) second {
    printf("second\n");
}
-(void) first {
    NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
    printf("first\n");

    [self performSelector:@selector(second) withObject:nil afterDelay:3];

    printf("ok\n");
    [apool release];
}

but the second method is never called, why? and, how may i accomplish my goal?

thanks


You have to have a running run loop for performSelector:withObject:afterDelay: to work.


Your code executes first and, when first exits, the thread is gone. You need to run a run loop.

Add:

[[NSRunLoop currentRunLoop] run];

To the end of first.

0

精彩评论

暂无评论...
验证码 换一张
取 消