My app has a table view that loads data from an XML file and caches this data. Right now, the table can add new sections when the XML file is updated, but I’m having trouble figuring out how to remove/overwrite the oldest sections of the table at the same time.
Here’s the fetchEntries part of the table view controller file:
- (void)fetchEntries {
// Initiate the request...
NSURL *url = [NSURL URLWithString:@"http://www.test.com"];
channel = [[FeedStore sharedStore] fetchRSSFeedWithURL:url completion:
^(RSSChannel *obj, NSError *err) {
if(!err) {
// How many items are there currently?
int currentItemCount = [[channel items] count];
// Set our channel to the merged one
channel = obj;
// How many items are there now?
int newItemCount = [[channel items] count];
// For each new item, insert a new row.
int itemDelta = newItemCount - currentItemCount;
if(itemDelta > 0) {
NSMutableIndexSet *stuff = [NSMutableIndexSet indexSet];
for(int i = 0; i < itemDelta; i++)
[stuff addIndex:i];
[[self tableView] insertSections:stuff withRowAnimation:UITableViewRowAnimationNone];
}
}
}];
[[self tableView] reloadData];
}
Another option would be to check a version number, and if the XML has been updated, delete the cache and table completely, and reload the data. But when I call this:
- (void)deleteCache {
NSString *cachePath =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:cachePath error:NULL];
[[channel items] removeAllObjects];
[self fetchEntries];
}
… the table view doesn’t get updated with the new data until I restart the app.
Any ideas?
Prasad got this correct – I was deleting the entire cache directory instead of the individual file.