问题描述
我仅通过设备收到以下警告:
I am getting the following warning with Device only:
CoreAnimation: warning, deleted thread with uncommitted CATransaction; created by:
0 QuartzCore 0x3763c65d <redacted> + 220
1 QuartzCore 0x3763c541 <redacted> + 224
2 QuartzCore 0x3763fe2f <redacted> + 30
3 QuartzCore 0x3763b9bf <redacted> + 318
4 QuartzCore 0x3763b87b <redacted> + 50
5 QuartzCore 0x3763b80b <redacted> + 538
6 MyApp 0x000df597 -[CommonClass orangeGradient:] + 382
7 MyApp 0x000f70e1 -[HomeViewController updateStatusBar] + 1700
8 MyApp 0x000f61bd -[HomeViewController statusRecieved] + 224
9 MyApp 0x000cd323 -[CommonClass statusReceivedFromServer] + 142
10 MyApp 0x000d833d -[CommonClass accountStatus] + 7416
11 Foundation 0x35a3767d <redacted> + 972
12 libsystem_c.dylib 0x35c9a311 <redacted> + 308
13 libsystem_c.dylib 0x35c9a1d8 thread_start + 8
位于堆栈顶部的我的方法如下:
My Method that is on top of the stack is as follows:
- (void)orangeGradient: (UILabel *)fillLabel {
@synchronized([CommonClass class]) {
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = fillLabel.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColorFromRGB(0xfbb250) CGColor],(id)[UIColorFromRGB(0xf47c2a) CGColor], nil];
[fillLabel.layer addSublayer:gradientLayer];
}
}
关于为什么会这样以及如何解决这个问题的任何想法?
Any idea on why is this coming, and how I can fix this?
推荐答案
看起来orangeGradient:
是从后台线程调用的.核心动画将所有更改分组到CATransactions中.通常,这是从运行循环中自动完成的.在后台线程上(通常)没有运行循环,因此您必须自己创建事务:
It looks like orangeGradient:
is called from a background thread. Core Animation groups all changes into CATransactions. Usually this is done automatically from the run loop. On background threads there is (usually) no run loop, so you have to create the transaction yourself:
- (void)orangeGradient: (UILabel *)fillLabel {
@synchronized([CommonClass class]) {
[CATransaction begin];
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = fillLabel.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColorFromRGB(0xfbb250) CGColor],(id)[UIColorFromRGB(0xf47c2a) CGColor], nil];
[fillLabel.layer addSublayer:gradientLayer];
[CATransaction commit];
}
}
还有另一个问题:UIKit不是线程安全的.您不能在后台线程的UILabel
上调用bounds
.
There's another issue: UIKit is not thread safe. You can't call bounds
on a UILabel
on a background thread.
这篇关于核心动画警告:“未提交的CATransaction"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!