3

就在 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
}

在管理器控制台中,完成后,我收到以下警告:

CoreAnimation: warning, deleted thread with uncommitted CATransaction;
4

3 回答 3

8

在非主线程上使用 UIKit UI API 时最常见此错误。您不必直接使用 Core Animation 来查看。所有 UIView 都由 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
});
于 2013-10-23T16:14:11.023 回答
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:请参阅主队列上的 performSelectorOnMainThread 和 dispatch_async 之间的区别是什么?

于 2013-11-04T23:53:35.070 回答
0

我发现了问题:正在更改 menuViewController 中的标签

在这个线程中,我使用了一个不是 de menuViewController 的变量:

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

如果我评论此行,则不会出现警告

(现在我必须找出如何在没有警告的情况下更改此标签)

于 2013-10-23T17:36:00.257 回答