進入我的計時器隨機不工作的問題。我可以從字面上停止應用程序並重建它,而無需更改任何代碼。它有時有效,有時不會? 當我說「有時」時,我正在討論每個應用程序會話。如果啓動,它將繼續運行。但它沒有運行每個應用程序會話。 在使用下面的兩個代碼塊時,我看到了同樣的情況。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中殺死應用程序後可能沒有正確等待? –