似乎我在iOS應用程序中的多任務處理有問題。 我有一個NSTimer正在運行,它會更新屏幕上的一些內容,另一個NSTimer將在定義的時間間隔後僅被觸發一次。應用程序後的黑色屏幕WillEnterForeground
問題是:我想對那個火災事件做出反應。無論我的App是否在前臺。
我用下面的代碼能夠跟蹤定時器:
-(void)backgroundThreadStarted {
NSAutoreleasePool* thePool = [[NSAutoreleasePool alloc] init];
// create a scheduled timer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(backgroundThreadFire:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
m_isBackgroundThreadToTerminate = NO;
// create the runloop
double resolution = 300.0;
BOOL isRunning;
do {
// run the loop!
NSDate* theNextDate = [NSDate dateWithTimeIntervalSinceNow:resolution];
isRunning = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:theNextDate];
// occasionally re-create the autorelease pool whilst program is running
[thePool release];
thePool = [[NSAutoreleasePool alloc] init];
} while(isRunning==YES && m_isBackgroundThreadToTerminate==NO);
[thePool release];
}
-(void)backgroundThreadFire:(id)sender {
// do repeated work every one second here
// when thread is to terminate, call [self backgroundThreadTerminate];
}
-(void)backgroundThreadTerminate {
m_isBackgroundThreadToTerminate = YES;
CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]);
}
(這裏找到:http://www.cocoadev.com/index.pl?RunLoop)
,我號召- (void)applicationWillEnterForeground:(UIApplication *)application
backgroundThreadStarted
上-(void)applicationDidEnterBackground:(UIApplication *)application
和backgroundThreadTerminate
和我能夠跟蹤定時器。所以這個效果很好。
不幸的是,回到應用程序後,整個屏幕是黑色的。我嘗試了不同的解決方案,我搜索了很多,但都沒有成功。
如果我在看到這個黑屏時將應用程序最小化,我可以在動畫到背景時看到該應用程序。
我錯過了什麼?
如果我不做這個backgroundThread的東西,屏幕顯示正常。但之後,我無法跟蹤計時器。
在此先感謝!
你在那個線程上做了什麼樣的工作? – 2011-05-11 18:20:55
只需等待定時器啓動。然後保存一些設置並停止播放音頻 - 這就是計劃。 – ChaosCoder 2011-05-12 08:53:29