gpt4 book ai didi

multithreading - NSOperation 子类中的 NSTimer

转载 作者:行者123 更新时间:2023-12-03 17:36:27 26 4
gpt4 key购买 nike

亲爱的社区。我尝试设置 NSTimer:

@interface GetExternalInfo : NSOperation {

NSTimer *keepAliveTimerMain;

@property (nonatomic,retain) NSTimer *keepAliveTimerMain;

.m:

@synthesize keepAliveTimerMain

-(void) main;
{
self.keepAliveTimerMain = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(keepAlive:) userInfo:nil repeats:YES];
[keepAliveTimerMain fire];
[[NSRunLoop currentRunLoop] addTimer:self.keepAliveTimerMain forMode: NSDefaultRunLoopMode];

BOOL timerState = [keepAliveTimerMain isValid];
NSLog(@"STAT:Timer Validity is: %@", timerState?@"YES":@"NO");


- (void)keepAlive:(NSTimer *)theTimer
{
BOOL currentState = [self isCancelled];
NSLog(@"STAT:cancelled state is %@.\n",currentState?@"YES":@"NO");
}

在日志中

2011-02-02 18:58:31.041 snow[54705:5d07] STAT:cancelled state is NO. 2011-02-02 18:58:31.042 snow[54705:5d07] STAT:Timer Validity is: YES

我只看到过一次。每 5 秒不再重复尝试对于这种情况有什么意见吗? GC 已启用。

最佳答案

当前线程中有runloop吗?计时器需要一个运行循环才能触发。我注意到您手动调用 -fire 这解释了为什么调用 -keepAlive ,但这实际上并没有启动计时器。

关于multithreading - NSOperation 子类中的 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4877765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com