I have a UITableView whose dataSource is updated at random intervals in a very short amount of time. As more objects are opened, they are added to the tableView data source, and I insert a specific indexPath:
[self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates];
The data source is in the manager class, and a notification is published when it is changed.
- (void)addObjectToDataSource:(NSObject*)object { [self.dataSource addObject:object]; [[NSNotificationCenter defaultCenter] postNotification:@"dataSourceUpdate" object:nil]; }
ViewController updates the tableView when it receives this notification.
- (void)handleDataSourceUpdate:(NSNotification*)notification { NSObject *object = notification.userInfo[@"object"]; NSIndexPath *indexPath = [self indexPathForObject:object]; [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates]; }
This works great, but I noticed that in some cases the second object is detected just like the first calls endUpdates, and I get an exception request. I have two objects in my data source when the tableView was expecting it.
I was wondering if anyone could figure out the best way to atomize row inserts into a tableView. I was thinking of placing the @synchronized block (self.tableView) around the update, but I would like to avoid this if possible, because it is expensive.
source share