Problems with C lens and GUI updates

I am developing an iOS application with a view containing a TableView. Some method receives data from the Internet, opens a new stream for calculating information, and inserts a row into the table at run time using the method: insertRowsAtIndexPaths .

Now, if a lot of data arrives at once, the table can be updated after several inserts, and not after each of them, and this raises an exception saying that the number of rows in the section is incorrect (this is because it thinks that it should have an increase of one row, but streams already inserted an array of data a few more cells).

Even if I do an insert lock on the data array and the insertRowsAtIndexPaths method, it still does the same thing.

 NSLock *mylock = [[NSLock alloc] init]; [mylock lock]; [array addObject:object]; [tableView insertRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationLeft]; [mylock unlock]; 

help me please,

Thanks!

+4
source share
1 answer

you need to run this method in the main thread. All interaction with the user interface must be performed in 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 call it like this:

 [self addSomeObject:anObject]; 

You can change this call to the following:

 [self performSelectorOnMainThread:@selector(addSomeObject:) withObject:anObject waitUntilDone:NO]; 
+9
source

All Articles