我有这段代码:
- (void) fetchRecordsStaringFrom:(double)start limit:(double)limit whereFields:(NSArray*)whereFields haveValues:(NSArray*)whereValues sortBy:(NSString*)sortField ascending:(BOOL)isascending delegateEvent:(BOOL)delegateEvent{
NSFetchRequest *_fetchRequest = [[NSFetchRequest alloc] init];
[_fetchRequest setEntity:self.entityDescription];
// Create the sort descriptors array.
// Default sort is ID
if (sortField == NULL) {
sortField = @"id";
}
NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:sortField ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, nil];
[_fetchRequest setSortDescriptors:sortDescriptors];
if (whereFields != Nil && whereValues != Nil && ([whereFields count] == [whereValues count])) {
NSMutableString *predicateString = [[NSMutableString alloc] initWithString:@""];
for (int index = 0; index < [whereFields count]; index++) {
[predicateString appendFormat:@"%@==%@", whereFields[index], whereValues[index]];
if (index < [whereFields count] - 1) {
[predicateString appendString:@" AND "];
}
}
// Delete old cache to make sure always get new data
[NSFetchedResultsController deleteCacheWithName:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
[_fetchRequest setPredicate:predicate];
} else {
}
self.fetchRequest = _fetchRequest;
self.fetchResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:self.fetchRequest managedObjectContext:self.context sectionNameKeyPath:nil cacheName:nil];
self.fetchResultController.delegate = self;
NSError *_error;
if (delegateEvent) {
if ([self.delegate respondsToSelector:@selector(CoreDataEntityRequestStartEvent:::)]) {
[self.delegate CoreDataEntityRequestStartEvent:self :self.fetchRequest :self.fetchResultController];
}
if (![self.fetchResultController performFetch:&_error]) {
if ([self.delegate respondsToSelector:@selector(CoreDataEntityRequestErrorEvent::)]) {
[self.delegate CoreDataEntityRequestErrorEvent:self :_error];
}
} else {
if ([self.delegate respondsToSelector:@selector(CoreDataEntityRequestSuccessEvent:::)]) {
[self.delegate CoreDataEntityRequestSuccessEvent:self :self.fetchRequest :self.fetchResultController];
}
}
if ([self.delegate respondsToSelector:@selector(CoreDataEntityRequestEndEvent:::)]) {
[self.delegate CoreDataEntityRequestEndEvent:self :self.fetchRequest :self.fetchResultController];
}
} else {
[self.fetchResultController performFetch:&_error];
}
}
执行时,上面的方法将从 SQLITE 数据库中获取数据,这里没有问题,但是当我使用外部 SQLITE 编辑器或通过其他方式更改 SQLITE,然后使用相同的参数再次执行时,它无法获取新数据并保持旧数据数据,除非我重新启动应用程序。
我到处寻找,可能是因为缓存,但正如您所见,我已经清除了缓存,但仍然无法获取新数据。
感谢您的回复,答案是:仅供测试,
这是实际情况:例如,在屏幕UITableView中,从互联网获取数据(数据不完整),将其存储在数据库中然后加载并显示在视图中,然后当点击一个单元格时,将从互联网获取完整数据以供选择单元格,更新数据库,并将其显示在其他屏幕上。当返回 UItableView 并从数据库重新加载时,由于之前的更新,记录集应该被更改,所以当用户在同一个单元格上重新点击时,不需要从互联网获取数据,因为我们已经完成了选定单元格的数据。但是....正如我在上面的问题中所解释的,NSFetchResultController 没有提供新数据,即使在 SQLITE 文件更新之后!