如果不使用CoreAnimation,如何避免"CoreAnimation警告已删除的线程与未提交的CATransaction"

Alb*_*alà 3 multithreading objective-c ios ios6

就在appdelegates,applicationDidBecomeActive.我创建并启动一个线程,该线程等待异步下载,然后保存数据:

 - (void)applicationDidBecomeActive:(UIApplication *)application
    {
              // begins Asynchronous download data (1 second):
               [wsDataComponents updatePreparedData:NO];

               NSThread* downloadThread = [[NSThread alloc] 
                  initWithTarget:self 
                        selector: @selector (waitingFirstConnection) 
                          object:nil];
               [downloadThread start];
        }
Run Code Online (Sandbox Code Playgroud)

然后

-(void)waitingFirstConnection{    

    while (waitingFirstDownload) {
      // Do nothing ...  Waiting a asynchronous download, Observers tell me when
      // finish first donwload
    }

    // begins Synchronous download, and save data (20 secons)
    [wsDataComponents updatePreparedData:YES];

    // Maybe is this the problem ?? I change a label in main view controller 
    [menuViewController.labelBadgeVideo setText:@"123 videos"];

    // Nothig else, finish and this thread is destroyed
}
Run Code Online (Sandbox Code Playgroud)

在Organizer控制台中,完成后,我收到此警告:

CoreAnimation: warning, deleted thread with uncommitted CATransaction;
Run Code Online (Sandbox Code Playgroud)

And*_*sen 8

在非主线程上使用UIKit UI API时,最常出现此错误.您不必直接使用Core Animation来查看此内容.所有UIView都支持Core Animation层,因此无论您是否直接与Core Animation进行交互,Core Animation都在使用.

你的问题中没有足够的代码来确定确切的问题,但你使用多线程的事实是一个线索,你的问题就像我所描述的那样.您是在下载完成后和/或保存数据后更新UI吗?如果是这样,您需要将UI更新移回主线程/队列.如果您使用GCD而不是NSThread,这会更容易:

// download is finished, save data
dispatch_async(dispatch_get_main_queue(), ^{
    // Update UI here, on the main queue
});
Run Code Online (Sandbox Code Playgroud)

  • 是的,问题是您是在后台线程中设置标签上的文本.对于一些相当狭窄的异常,不允许使用后台线程中的UIKit类.任何更新或操作应用程序用户界面的操作都需要在主线程/队列上完成. (2认同)