我收到一个未提交的CATransaction警告,我无法解决问题。我的应用程序工作得很好它正在做我期望的每件事,屏幕更新标签我想要的速度,它看起来都很好。
Dec 10 10:40:10 my-iPhone myAppName[5967] <Warning>: CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces.
我设置了CA_DEBUG_TRANSACTIONS = 1并找到了以下列表。
Dec 10 10:43:45 my-iPhone myAppName[5994] <Warning>: CoreAnimation: warning, deleted thread with uncommitted CATransaction; created by:
0 QuartzCore 0x348fc65d <redacted> + 220
1 QuartzCore 0x348fc541 <redacted> + 224
2 QuartzCore 0x348fc325 <redacted> + 24
3 QuartzCore 0x34900489 <redacted> + 44
4 QuartzCore 0x34905091 <redacted> + 456
5 QuartzCore 0x34904ebf <redacted> + 42
6 QuartzCore 0x34904e91 <redacted> + 44
7 myAppName 0x000d9ec1 -[MainViewController updateProgress:] + 56
8 Foundation 0x3a75067d <redacted> + 972
9 libsystem_c.dylib 0x397d6311 <redacted> + 308
10 libsystem_c.dylib 0x397d61d8 thread_start + 8
导致此警告的代码如下所示:
- (void)updateProgress:(NSString*) myLabelString
{
//this is updating a UILabel on the main view
[myLabel setText:myLabelString];
}
从for循环调用它,我调用performSelectorinBackground:
for (;;) {
// Do stuff here to set up the info for the string
[self performSelectorInBackground:@selector(updateProgress:) withObject:myLabelString];
usleep(100000);
if (stopLoop == YES) {
// do stuff before we exit for loop
break;
}
}
我已经尝试了几个主要的想法,确保在退出选择器“updateProgress”之前确保对myLable的更新已完成,唯一的作用是将时间(+56)更改为更大的数字。 我也尝试过使用:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0f];
// edit UILabel here
[UIView commitAnimations];
我从另一篇文章中尝试了这个,但由于我没有使用核心动画,因此编译器反对“CATransaction”作为未声明的标识符,我并不感到惊讶。
[CATransaction begin];
[CATransaction setDisableActions:YES];
//
[CATransaction commit];
有关如何确定标签更新是否完整的任何建议?正如我所说的应用程序正在运行,我只需要清除此警告。
答案 0 :(得分:3)
此问题已在开发者论坛上得到解答。 事实证明我在后台线程上做UIKit工作。
我换了一行:
[self performSelectorInBackground:@selector(updateProgress:) withObject:myLabelString];
要:
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:myLabelString waitUntilDone:NO];
我尝试将WaitUntilDone设置为YES,这也有效。
答案 1 :(得分:1)
确保在主线程上进行任何UI绘制的另一种方法,如Stephen所描述,使用函数dispatch_async()
// Perform all drawing/UI updates on the main thread.
dispatch_async(dispatch_get_main_queue(), ^{
// Perform any drawing/UI updates here.
});
有关dispatch_async()
和performSelectorOnMainThread:withObjects:waitUntilDone:
之间差异的相关帖子,请参阅Whats the difference between performSelectorOnMainThread and dispatch_async on main queue?