就在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];
}
然后
-(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
}
在Organizer控制台中,完成后,我收到此警告:
CoreAnimation: warning, deleted thread with uncommitted CATransaction;
答案 0 :(得分:8)
在非主线程上使用UIKit UI API时,最常出现此错误。您不必直接使用Core Animation来查看此内容。所有UIViews都由核心动画层支持,因此无论您是否直接与其进行交互,都会使用Core Animation。
在你的问题中没有足够的代码来确定确切的问题,但你使用多线程的事实是一个线索,你的问题正如我所描述的那样。您是在下载完成后和/或保存数据后更新UI吗?如果是这样,您需要将UI更新移回主线程/队列。如果您使用GCD而不是NSThread,这会更容易:
// download is finished, save data
dispatch_async(dispatch_get_main_queue(), ^{
// Update UI here, on the main queue
});
答案 1 :(得分:3)
如Andrew所述,确保在主线程上进行任何UI绘制的另一种方法是使用方法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:
之间差异的相关帖子,请参阅Whats the difference between performSelectorOnMainThread and dispatch_async on main queue?
答案 2 :(得分:0)
我发现了问题:正在更改menuViewController中的标签
在这个帖子中,我使用了一个isntance变量而不是de menuViewController:
[menuViewController.labelBadgeVideo setText:@"123 videos"];
如果我对此行发表评论,则不会出现警告
(现在我必须在没有警告的情况下找出如何更改此标签)