我正在尝试解决围绕 NSTimer/NSOperationQueue 执行不一致的 UI 问题。我看到无论我使用 NSTimer 还是 NSInvocationOperation 来触发下面的代码,很多时候性能是需要的,但有几次行为很慢(如下例所示,代码有时会运行超过 1 秒)。
这是我通过 NSInvocationOperation 调用的代码:
-(void) progressAsynch{
for (count = 1; count<=100; count++) {
// Reposition Label's CGRect.
[self performSelectorOnMainThread:@selector(moveLabelToNewPosition) withObject:nil waitUntilDone:YES];
//Update the progress of the progress view
[self performSelectorOnMainThread:@selector(advanceProgressViewProgress) withObject:nil waitUntilDone:YES];
//Sleep for 10 millisecs
[NSThread sleepForTimeInterval:0.01];
//if the progress view has progressed fully call main thread to to next tasks.
if(progressView.progress == 1) {
[self performSelectorOnMainThread:@selector(processResult) withObject:nil waitUntilDone:YES];
}
}
}
NSTimer(每 10 毫秒触发一次)调用的方法的代码与上面非常相似,只是它没有 for 循环。
显然,似乎在这个处理之外有一些东西经常减慢这种性能。
很想知道您是否遇到过类似的问题,或者您是否有任何可以帮助我的提示/花絮。
提前致谢..