进入我的计时器随机不工作的问题。我可以从字面上停止应用程序并重建它,而无需更改任何代码。它有时有效,有时不会? 当我说“有时”时,我正在讨论每个应用程序会话。如果启动,它将继续运行。但它没有运行每个应用程序会话。 在使用下面的两个代码块时,我看到了同样的情况。NSTimer有时仅会触发?
//Code1:
//I tried this block of code after reading it might be related to how I am interacting with my UIElements while the timer is running in the background.
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
syncWithServerTimer = [NSTimer timerWithTimeInterval:15 target:self selector:@selector(syncWithServer) userInfo:nil repeats:YES];
[runloop addTimer:syncWithServerTimer forMode:NSRunLoopCommonModes];
[runloop addTimer:syncWithServerTimer forMode:UITrackingRunLoopMode];
//Code2:
syncWithServerTimer = [[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(syncWithServer) userInfo:nil repeats:YES] retain];
同样,我想这两个,他们都工作的大部分时间,但不是所有的时间。 可能是什么问题?我不会过早释放或失效。
您在哪种方法中执行计时器初始化(您发布的代码)? – sergio
嗨@sergio它在-viewDidLoad – Louie
没有看到更多的代码,这是不可能的。这两者看起来像插入到-viewDidLoad时会很好地工作。在启动另一个实例之前,您在Xcode中杀死应用程序后可能没有正确等待? –