开发者

Objective C threads and GUI updates problem

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).开发者_如何学C

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!


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:

- (void)addSomeObject:(id)object {
    [array addObject:object];
    [tableView insertRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationLeft];
}

and you are calling it like this:

[self addSomeObject:anObject];

then you would change this call to something like this:

[self performSelectorOnMainThread:@selector(addSomeObject:) withObject:anObject waitUntilDone:NO];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜