[NSDate timeIntervalSinceReferenceDate]上的exc_bad_access

时间:2011-10-21 17:11:42

标签: ios nsdate exc-bad-access

我从[NSDate timeIntervalSinceReferenceDate]获得了奇怪的行为。

我有以下功能:

-(void) insertRow{
NSTimeInterval timeNow = [NSDate timeIntervalSinceReferenceDate];
if (timeNow - secondsSinceTableViewScroll <0.5){
    [self insertRow];
    return;
}

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:[self.itemsToFollow count] - 1];
[self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

这是来自ASIHTTPRequest requestFinished的回调。如果我在代码中放置一个断点,它可以正常工作。如果我只是尝试运行代码,我会在行上找到exc_bad_access

NSTimeInterval timeNow = [NSDate timeIntervalSinceReferenceDate];

secondsSinceTableViewScroll是标题中声明的ivar,设置如下:

 secondsSinceTableViewScroll = [NSDate timeIntervalSinceReferenceDate];

任何想法为什么我没有断点时得到exc_bad_access?

由于

我唯一能找到的东西

我正在检查这样的时间:

-(void) insertRow{
end = [NSDate timeIntervalSinceReferenceDate];
if(end-start < 0.05){
    [self insertRow];
    return;
}

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:[self.itemsToFollow count] - 1];
[self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

我肯定在某个地方它说你不能背对着timeIntervalSinceReferenceDate的调用,这就是它崩溃的原因(这种递归循环无论如何都是一个非常糟糕的主意)。

所以使用while循环,或者更好的是NSTimer。

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您的症状表明您实际上正在崩溃另一个线程。你有其他线程在运行吗?检查所有线程的回溯。以上是主线程,对吗?


编辑:你的复制速度太快,堆栈溢出。 “尽可能快地给自己打电话”的半秒钟会很快溢出你的筹码。

答案 1 :(得分:2)

更改

[self insertRow];

[self performSelector:@selector(insertRow) withObject:nil afterDelay:0];