开发者

Black screen after applicationWillEnterForeground

seems I have a problem with multitasking in my iOS App. I have a NSTimer running, which updates some things on the screen开发者_如何学Python and another NSTimer which will be fired only once after a defined time interval.

The problem is: I want to react on that fire-event. Whether my App is in the foreground or not.

I used the following code to be able to track the timer:

-(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]);
}

(found here: http://www.cocoadev.com/index.pl?RunLoop)

I'm calling backgroundThreadStarted on -(void)applicationDidEnterBackground:(UIApplication *)application and backgroundThreadTerminate on - (void)applicationWillEnterForeground:(UIApplication *)application and I am able to track the timer. So this works great.

Unfortunately after returning back to the app, the whole screen is black. I tried different solutions and I googled a lot but neither worked.

If I minimize the App while seeing this black screen I am able to see the App while it is animating to the background.

What am I missing?

If I don't do this backgroundThread stuff, the screen shows up normal. But then, I'm not able to track the timer.

Thanks in advance!


You're using code for AppKit (OS X) not UIKit (iOS).

You should be looking into the backgrounding API which would involve handling didEnterBackground and in there creating a background task with beginBackgroundTaskWithExpirationHandler:

Background threads aren't necessary (they're for things running in the background of your application, while your application is still running). The background tasks are a way of letting your regular application code run (sans UI interaction) when the application is no longer in the foreground. You will not be able to easily communicate with the user about this (the API is meant more for letting an upload finish after leaving an application), outside of the local notification service. And your task is limited to ten minutes after being backgrounded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜