Objective C 线程和 GUI 更新问题
我正在开发一个 iOS 应用程序,其视图包含 TableView。 某些方法从网络接收数据,打开一个新线程来计算信息,并在运行时使用以下方法将一行插入表中:insertRowsAtIndexPaths
。
现在,如果同时出现大量数据,表可能会在几次插入后自行更新,而不是每次插入后更新,这会引发异常,表明节中的行数不正确(这是因为它认为应该增量为一行,但线程已经将数据数组插入了更多单元格)。
即使我对数据源数组的插入和 insertRowsAtIndexPaths 方法进行锁定,它仍然会执行相同的操作。
NSLock *mylock = [[NSLock alloc] init];
[mylock lock];
[array addObject:object];
[tableView insertRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationLeft];
[mylock unlock];
请帮忙,
谢谢!
I'm developing an iOS app with a view containing a TableView.
Some method receives data from the web, opens a new thread to calculate information and inserts a row into the table at run time with the method: insertRowsAtIndexPaths
.
Now if a lot of data is coming at once, the table may update itself after a few insertions and not after each one, and thats provokes an exception saying that the number of rows in section isn't right (that's because it thinks it should have an increment of one row but the threads already inserted the array of data some more cells).
Even if I make a lock on the insertion to the datasource array and the insertRowsAtIndexPaths
method, it's still do the same.
NSLock *mylock = [[NSLock alloc] init];
[mylock lock];
[array addObject:object];
[tableView insertRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationLeft];
[mylock unlock];
help please,
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须在主线程上运行这个方法。所有用户界面交互都必须在主线程上完成。
假设您的方法如下所示:
并且您这样调用它:
那么您可以将此调用更改为如下所示:
you have to run this method on the main thread. All User Interface interaction has to be done on the main thread.
Let's say your method looks like this:
and you are calling it like this:
then you would change this call to something like this: