gpt4 book ai didi

iphone - applicationWillEnterForeground 后黑屏

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:36 24 4
gpt4 key购买 nike

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

问题是:我想对那个火灾事件使用react。我的 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)applicationDidEnterBackground:(UIApplication *)application 上调用 backgroundThreadStarted 并在 上调用 backgroundThreadTerminate-(void )applicationWillEnterForeground:(UIApplication *)application 并且我能够跟踪计时器。所以这很好用。

不幸的是,回到应用程序后,整个屏幕都是黑色的。我尝试了不同的解决方案,并在谷歌上搜索了很多,但均无济于事。

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

我错过了什么?

如果我不执行此 backgroundThread 操作,屏幕显示正常。但是,我无法跟踪计时器。

提前致谢!

最佳答案

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

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

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

关于iphone - applicationWillEnterForeground 后黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5963659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com