如何正确停止NSRunLoop?

时间:2011-11-26 11:48:09

标签: iphone objective-c nsrunloop

我在我的应用程序中使用NSRunLoop。只有一个陈述

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]]; 

在for循环中执行。需要每两秒更新一次文本三次。一切正常,但当我退出运行的屏幕(当它运行时,这是最重要的),然后再次进入屏幕,然后再次退出我的应用程序崩溃。我假设我必须停止运行NSRunLoop如果我在运行时退出但我不知道该怎么做。你能告诉我什么吗?

这是我的循环:

for (int i = 1;i <= 3;i++)
{
    int result = [self analyzeCards];

   ((UILabel *)[bidLabels objectAtIndex:i]).text = [bidResults objectAtIndex:result];


    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]]; 

    bidIndexes [i - 1] = result;
}

1 个答案:

答案 0 :(得分:1)

感谢您修改问题以包含一些源代码。

我建议您不要执行您正在执行的runUntilDate事务,而是将其作为分离线程执行(making sure to actually do the updating the labels -- the user interface -- on the main thread)。

崩溃本身很有可能发生,因为bidLabels数组中的对象是陈旧的或已释放的,或者仅仅是垃圾。每次重新打开窗口时重置该数组。