相关疑难解决方法(0)

如何在后台线程上创建NSTimer?

我有一项需要每1秒执行一次的任务.目前我每隔1秒就有一次NSTimer重复射击.如何在后台线程(非UI线程)中触发计时器?

我可以在主线程上使用NSTimer激发然后使用NSBlockOperation来调度后台线程,但我想知道是否有更有效的方法来执行此操作.

cocoa objective-c nstimer nsrunloop nsblockoperation

66
推荐指数
6
解决办法
6万
查看次数

iPhone SDK应用程序中的NSThread,NSTimer和AutoreleasePools

我想在iPhone中创建一个我想使用NSThread的appilication.我用了创建了一个线程

[NSThread detachNewThreadSelector:@selector(doThread:)
                             toTarget:self
                           withObject:nil];
Run Code Online (Sandbox Code Playgroud)

我希望我的一个线程将处理所有的触摸和其他用户交互,第二个线程处理NSTimer.所以,在doThread()中,我已经分配了NSTimer,

-(void) doThread:(NSString *)poststring {

    NSLog(@"create thread:");

    [lock lock];
    T1 = [NSTimer scheduledTimerWithTimeInterval:(5)            
     target : self
     selector:@selector(onTimer)
     userInfo : nil
     repeats : YES];
     NSLog(@"after timer");

    usleep(1);
    [lock unlock];
}

In onTImer,

-(void)onTimer

{
    NSLog(@"in timer");

}
Run Code Online (Sandbox Code Playgroud)

现在我无法调用NSTimer的onTimer方法.但我可以看到日志中打印出"after timer".是不是我在线程中无法使用NSTimer?

这也是我执行时可以得到的.

NSAutoreleaseNoPool(): Object 0xd15880 of class __NSCFDate autoreleased with no pool in place - just leaking
Stack: (0x305a2e6f 0x30504682 0x30525acf 0x27b5 0x3050a79d 0x3050a338 0x926ae155 0x926ae012)
Run Code Online (Sandbox Code Playgroud)

请帮助我.谢谢.

iphone cocoa multithreading timer objective-c

1
推荐指数
1
解决办法
4610
查看次数