你的怀疑是对的。如果 NSOperation 在 CoreAnimation 执行完成之前完成,那么你会收到一个很好的警告:
*CoreAnimation:警告,删除了未提交 CATransaction 的线程;在环境中设置 CA_DEBUG_TRANSACTIONS=1 以记录回溯。*
这也可能发生在某些情况下,当在队列上调度的块触发 CoreAnimation 的某些工作并在 CoreAnimation 完成之前返回。
我使用的解决方案很简单:在请求 CoreAnimation 工作的块或 NSOperation 上,我在退出之前检查工作是否确实已完成。
给你一个概念验证的例子,这是一个在调度队列上调度的块。为了避免警告,我们在退出前检查 CoreAnimation 是否完成。
^{
// 1. Creating a completion indicator
BOOL __block animationHasCompleted = NO;
// 2. Requesting core animation do do some work. Using animator for instance.
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
[[object animator] perform-a-nice-animation];
} completionHandler:^{
animationHasCompleted = YES;
}];
// 3. Doing other stuff…
…
// 4. Waiting for core animation to complete before exiting
while (animationHasCompleted == NO)
{
usleep(10000);
}
}