我正在运行到索引超出范围例外之一,在我的UITableViews
和我认为它可能归结为一些多线程问题。这就是我认为正在发生的事情:UITableViewDataSource和多线程
- 我有一个
UITableView
和它的数据源是一个普通NSMutableArray
。 - 这个支持我的
UITableView
的NSMutableArray
每隔几秒就会更新一次API响应的内容。 - 每次更新后,都会调用
UITableView
的reloadData以确保用户看到来自API服务器的新数据。 - 有时超出界限异常的索引被抛出。
这里是我的代码:
-(NSMutableArray*) currentBetEvents
{
return currentMarketId == nil ? [[BFOpenBetsModel sharedInstance] betEvents] : filteredBetEvents;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSArray *betEvents = [self currentBetEvents];
return [betEvents count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *betEvents = [self currentBetEvents];
id obj = [betEvents objectAtIndex:indexPath.section] // this is where it blows up
基本上,我得到一个异常,而试图在索引来访问betEvents结构的对象0
我相信的情况是:
reloadData
被调用UITableView
numberOfSectionsInTableView:
被调用,返回的值大于0.- 胭脂线程到达并清除了
UITableView
的数据源。 cellForRowAtIndexPath:
被调用并炸弹。
有什么方法可以确保不会发生这种情况吗?我是否需要开始在数据源上使用一些原始锁,以确保在更新表时不会更新?
编辑 拿了再看看由currentBetEvents返回的数据结构可以如何改变,它看起来像filteredBets & betEvents可以如下面的代码,结果被清除出:
[[NSNotificationCenter defaultCenter] postNotificationName:kUserLoggedOutNotification object:nil];
每当用户注销时都会发布此通知。每当用户注销应用程序时,我需要清除filteredBets和betEvents数组。是有可能,以下可能发生:
reloadData
被呼吁UITableView
numberOfSectionsInTableView:
调用它返回一个值> 0- 用户注销揭开序幕通知&送走数据结构。
cellForRowAtIndexPath:
被调用并炸弹。
感谢,
肖恩
你正在实现numberOfRowsInTableView? – rdelmar
我认为你需要展示betEvents如何得到更新,以及你如何通知表视图它需要更新。 – rdelmar
rdelmar - 我没有实现numberOfRowsInTableView。只需numberOfRowsInSection和numberOfSectionsInTableView。编辑我的答案,以包含更多关于赌注数组如何更新的更多信息。谢谢。 – seanoshea