applicationWillEnterForeground后黑屏

发布于 2024-11-06 02:57:01 字数 2001 浏览 0 评论 0原文

我的 iOS 应用程序似乎存在多任务处理问题。 我有一个 NSTimer 正在运行,它会更新屏幕上的一些内容,另一个 NSTimer 只会在定义的时间间隔后触发一次。

问题是:我想对火灾事件做出反应。我的应用程序是否在前台。

我使用以下代码来跟踪计时器:(

-(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)applicationDidEnterBackground:(UIApplication *)applicationbackgroundThreadTerminate 上调用 backgroundThreadStarted- (void)applicationWillEnterForeground:(UIApplication *)application 上,我能够跟踪计时器。所以这效果很好。

不幸的是,返回应用程序后,整个屏幕都是黑色的。我尝试了不同的解决方案,并且用谷歌搜索了很多,但都不起作用。

如果我在看到这个黑屏时最小化应用程序,我就可以在它动画到背景时看到该应用程序。

我缺少什么?

如果我不做这个backgroundThread的东西,屏幕显示正常。但是,我无法跟踪计时器。

提前致谢!

seems I have a problem with multitasking in my iOS App.
I have a NSTimer running, which updates some things on the screen 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-11-13 02:57:01

您使用的是 AppKit (OS X) 的代码,而不是 UIKit (iOS) 的代码。

您应该研究后台 API,其中涉及处理 didEnterBackground 并在其中使用 beginBackgroundTaskWithExpirationHandler 创建后台任务:

后台线程不是必需的(它们用于在应用程序后台运行的事物,当您的应用程序仍在运行时)。后台任务是当应用程序不再位于前台时让常规应用程序代码运行(无需 UI 交互)的一种方式。在本地通知服务之外,您将无法轻松地与用户就此进行沟通(API 更多的是为了让上传在离开应用程序后完成)。并且您的任务仅限于后台运行后的十分钟内。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文