NSCache每次设置对象都会驱逐对象!这很疯狂。
以下是我添加对象的方法:
[self.inMemoryCache setObject:@{@"data" : data, @"filePath" : filePath} forKey:filePath];
这是我的委托方法willEvictObject:
-(void)cache:(NSCache *)cache willEvictObject:(id)obj
{
NSLog(@"evicted obj %@",[obj description]);
if ([obj isKindOfClass:[NSDictionary class]]) {
NSData *data = [obj objectForKey:@"data"];
NSString *filePath = [obj objectForKey:@"filePath"];
[data writeToFile:filePath atomically:YES];
}else if ([obj isKindOfClass:[NSArray class]]){
NSArray *viewControllerNames = obj;
[viewControllerNames writeToFile:[UINavigationController pathToData] atomically:YES];
}
}
每次我将对象添加到缓存中时都会将其驱逐出去。真奇怪。
有什么想法吗?以下是我如何分配它:
navigationController.inMemoryCache = [[NSCache alloc] init];
navigationController.inMemoryCache.delegate = navigationController;
谢谢!