我正在实现一个 iOS 应用程序。并发现有时屏幕会一点点变黑。 例如,在 View Controller 中,有一个 Collection View ,一个按钮,一个页面 Controller ,有时我发现 Collection View 变黑(或不可见),只显示黑色背景,1-2秒后,按钮是没了,然后全屏黑了。但如果我将应用程序置于后台,然后将其恢复,一切正常。
代码如下:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSTimeInterval timeLeft = [UIApplication sharedApplication].backgroundTimeRemaining - 1.0;
if(timeLeft < 0)
{
completionHandler(UIBackgroundFetchResultNoData);
}
//create new thread to do something, and sleep at current thread
...create new threads
[NSThread sleep:timeLeft];
completionHandler(UIBackgroundFetchResultNewData);
});
}
如果我多次重复以下操作,问题可能会重现:
- 做某事
- 将应用置于后台(按主页按钮)
- 将应用程序带回前台
- 重复 1 - 3
我们在应用程序的管理器中发现了以下错误: :CoreAnimation:警告,已删除未提交 CATransaction 的线程;在环境中设置 CA_DEBUG_TRANSACTIONS=1 以记录回溯。
添加后,得到如下日志:
Myapp[11496] : CoreAnimation: warning, deleted thread with uncommitted CATransaction; created by:
0 QuartzCore 0x31ca6a75 + 268
1 QuartzCore 0x31ca6929 + 224
2 QuartzCore 0x31cabddb + 86
3 QuartzCore 0x31cab9f7 + 150
4 UIKit 0x3203f919 + 344
5 UIKit 0x320bb11b + 138
6 UIKit 0x322b0ebf + 218
7 UIKit 0x322b1169 + 104
8 UIKit 0x322b1735 + 36
9 Myapp 0x002e538d __61-[AppDelegate application:performFetchWithCompletionHandler:]_block_invoke + 632
10 libdispatch.dylib 0x3a487d53 + 10
11 libdispatch.dylib 0x3a48d689 + 228
12 libdispatch.dylib 0x3a48d8dd + 56
13 libsystem_pthread.dylib 0x3a5b8c17 _pthread_wqthread + 298
14 libsystem_pthread.dylib 0x3a5b8adc start_wqthread + 8
我的问题是: application:performFetchWithCompletionHandler 怎么会导致动画问题?
回答问题: 1.我确定手机不会休眠 2.源代码。对不起,项目太大了
最佳答案
检查您是否仅在主线程上使用 UI,应该是这种情况。
编辑: - 我见过 this一个,虽然从未使用过它(由 Peter Steinberger 编写)。
还有 this answer可以帮助避免更多问题。
关于iOS屏幕一点点变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25320899/