我很惊讶,似乎没有人感兴趣。我猜想,每个处理大量动态创建的结果控制器的人都需要这个!
无论如何,这是我的解决方案:
// NSFetchedResultsController+GenericCache
- (id)initWithFetchRequest:(NSFetchRequest *)fetchRequest
managedObjectContext:(NSManagedObjectContext *)context
sectionNameKeyPath:(NSString *)sectionNameKeyPath
useCache:(BOOL)useCache {
NSString *cacheId = nil;
if (useCache) {
cacheId = [NSString stringWithFormat:@"%@-%@-%@"
, sectionNameKeyPath
, [fetchRequest entityName]
, [[fetchRequest predicate] predicateFormat]];
for (NSSortDescriptor *descriptor in [fetchRequest sortDescriptors]) {
NSString *sortId = [NSString stringWithFormat:@"-%@-%@-%@"
, [descriptor key]
, ([descriptor ascending]) ? @"ascending": @"descending"
, NSStringFromSelector([descriptor selector])];
cacheId = [cacheId stringByAppendingString:sortId];
}
}
return [self initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:sectionNameKeyPath
cacheName:cacheId];
}
- (void)deleteCache {
[[self class] deleteCacheWithName:[self cacheName]];
}
我仍在测试,我非常愿意接受批评和改进建议。
请帮助使其可靠。