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

就在appdelegates中,applicationDidBecomeActive。 我创build并启动一个线程,此线程等待asynchronous下载,然后保存数据:

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

然后

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

在组织者控制台,完成后,我收到此警告:

 CoreAnimation: warning, deleted thread with uncommitted CATransaction; 

如Andrew所述,确保任何UI绘图的另一种方法是使用方法performSelectorOnMainThread:withObject:waitUntilDone:或者执行performSelectorOnMainThread:withObject:waitUntilDone: performSelectorOnMainThread:withObject:waitUntilDone:modes:

 - (void) someMethod { […] // Perform all drawing/UI updates on the main thread. [self performSelectorOnMainThread:@selector(myCustomDrawing:) withObject:myCustomData waitUntilDone:YES]; […] } - (void) myCustomDrawing:(id)myCustomData { // Perform any drawing/UI updates here. } 

有关dispatch_async()performSelectorOnMainThread:withObjects:waitUntilDone:之间区别的相关文章,请参阅主队列上performSelectorOnMainThread和dispatch_async之间的区别是什么?

在非主线程上使用UIKit UI API时,最常出现此错误。 你不必直接使用核心animation来看这个。 所有的UIView都有一个核心animation层的支持,所以无论你是否直接与它交互,Core Animation都在使用中。

在你的问题中没有足够的代码来确定确切的问题,但是你使用multithreading的事实是你的问题的线索,正如我所描述的。 下载完成和/或数据保存后,您是否正在更新您的用户界面? 如果是这样,您需要将UI更新移回主线程/队列。 如果使用GCD而不是NSThread,则更容易:

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

我发现这个问题:改变menuViewController中的标签

在这个线程中,我使用的isntancevariables比de menuViewController:

 [menuViewController.labelBadgeVideo setText:@"123 videos"]; 

如果我评论这条线,不会出现警告

(现在我必须找出如何改变这个标签没有警告)