The SeismicXML parser sample code in the apple documentation does something similar. It gets 'live' (i.e. changing) information about earthquakes from a server and updates the datasource via an NSNotification when new information arrives. The UITableView reloads its data based on key-value observing of when the NSMutableArray changes. The reload is done with [tableView reloadData].
There is also user interaction with the table: the user can select a tableViewCell and choose to follow a link to further information.
If you're worried about controlling coordination:What you want to avoid is that the user taps on the table, the dataArray gets changed, and the -tableView:didSelectRowAtIndexPath:
method tries to access the data at the same time. There are two choices: either prevent selection in the table while the data updates or prevent the data from updating while the row selection was occurring.
You can prevent interaction with the table by setting tableView.allowsSelection=NO
. You can find out when the data is about to update by using the key-value-observing option: NSKeyValueObservingOptionPrior
. This approach generally loses points though because you may interrupt what the user wanted to do.
The other choice would be to delay the data update by setting up a couple of booleans: isSelecting and dataNeedsReloading. Flow would be something like:
the user taps the table, isSelecting=YES
If new data come in,
if (isSelecting) {
[tempArray addObject:newData];
dataNeedsReloading=YES;
}
Then when the selection process finishes, reset isSelecting=NO
, check the dataNeedsReloading
flag, combine the arrays and reload the table if needed. When finished, reset dataNeedsReloading=NO
. Also reset the tempArray with -removeAllObjects
.