XML文件中的缓存表视图存在问题



我的应用程序有一个表视图,它从XML文件加载数据并缓存这些数据。现在,当XML文件更新时,表可以添加新的部分,但我很难弄清楚如何同时删除/覆盖表中最旧的部分。

这是表视图控制器文件的fetchEntries部分:

- (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];
}

另一种选择是检查版本号,如果XML已经更新,则完全删除缓存和表,然后重新加载数据。但当我称之为:

- (void)deleteCache {
    NSString *cachePath =
    [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                     NSUserDomainMask,
                                     YES) objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:cachePath error:NULL];
    [[channel items] removeAllObjects];
    [self fetchEntries];
} 

在我重新启动应用程序之前,表视图不会用新数据更新。

有什么想法吗?

Prasad得到了正确的答案——我删除了整个缓存目录,而不是单个文件。

最新更新