xcode iOS 6:CoreAnimation:警告,已删除带有未提交 CATransaction 的线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13807955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:17:12  来源:igfitidea点击:

iOS 6 : CoreAnimation: warning, deleted thread with uncommitted CATransaction

objective-cxcodeios6

提问by Stephen_1724543

I am getting a uncommitted CATransaction warning that I can't seam to resolve. My app works fine it is doing every thing that I expect, the screen updates the label as fast as I need, it all looks good.

我收到一个未提交的 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.

I set the CA_DEBUG_TRANSACTIONS=1 and found the following listing.

我设置了 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

The code that is causing this warning looks like this :

导致此警告的代码如下所示:

   - (void)updateProgress:(NSString*) myLabelString
    {
    //this is updating a UILabel on the main view
    [myLabel setText:myLabelString];

    }

it is called from a for loop where I call performSelectorinBackground:

它是从我调用 performSelectorinBackground 的 for 循环调用的:

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;
    }

}

I have tried several things mostly with the idea of making sure that the update to myLable is complete before exiting the selector "updateProgress" , the only effect is to change the time (+56) to a larger number. I have also tried to use:

我尝试了几件事,主要是为了确保在退出选择器 "updateProgress" 之前完成对 myLable 的更新,唯一的效果是将时间 (+56) 更改为更大的数字。我也尝试使用:

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0f];    
    // edit UILabel here    
    [UIView commitAnimations];

And I tried this from a different post, but as I am not using core animations, I was not really surprised that the compiler objected to "CATransaction" as an undeclared identifier.

我在另一篇文章中尝试过这个,但由于我没有使用核心动画,编译器反对将“CATransaction”作为未声明的标识符,我并不感到惊讶。

      [CATransaction begin];
      [CATransaction setDisableActions:YES];
      //
      [CATransaction commit];

Any suggestions on how to determine if the label update is complete? As I said the app is working I just need to clear this warning.

关于如何确定标签更新是否完成的任何建议?正如我所说的应用程序正在运行,我只需要清除此警告。

采纳答案by Stephen_1724543

This question was answered on the Developer forum. it turns out that I was doing UIKit work on a background thread.

这个问题在开发者论坛上得到了回答。原来我是在后台线程上做 UIKit 工作。

I changed the line :

我改变了线路:

    [self performSelectorInBackground:@selector(updateProgress:) withObject:myLabelString];

To:

到:

    [self performSelectorOnMainThread:@selector(updateProgress:) withObject:myLabelString waitUntilDone:NO];

I tried setting WaitUntilDone to YES as well and that also worked.

我也尝试将 WaitUntilDone 设置为 YES 并且这也有效。

回答by Dalmazio

Another way of ensuring any UI drawing occurs on the main thread, as described by Stephen, is using the function dispatch_async()

确保任何 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.
});

For a related post on the difference between dispatch_async()and performSelectorOnMainThread:withObjects:waitUntilDone:see Whats the difference between performSelectorOnMainThread and dispatch_async on main queue?

有关两者之间区别的相关帖子dispatch_async()performSelectorOnMainThread:withObjects:waitUntilDone:请参阅主队列上 performSelectorOnMainThread 和 dispatch_async 之间的区别是什么?