[英]Best way to check if objects have been removed from NSCache?
Since NSCache is a black box as in: 由于NSCache是一个黑匣子,如下所示:
1) No system notification is sent when objects are removed due to memory pressure. 1)由于内存压力而移除对象时,不会发送系统通知。
2) The cache is not purged by the OS en total but pared down object-by-object after a memory threshold is reached. 2)缓存不会被OS整体清除,而是会在达到内存阈值后逐个对象进行缩减。
Is there a better way to check if a cached object still exists other than checking to see if it's empty like so: 除了检查缓存对象是否为空之外,还有没有其他更好的方法来检查缓存对象,如下所示:
NSArray *array = [[MyCache sharedCache] someArray];
if (array.count > 0)
{
//you're ok
}
else
{
[self repopulateCache];
}
Is MyCache a class you created? MyCache是您创建的类吗? With NSCache you could use
objectForKey
to check if something is still there. 使用NSCache,您可以使用
objectForKey
来检查是否还有东西。
use : 采用 :
if ([[ImageCache sharedImageCache] DoesExist:imageName] == YES)
{
image = [[ImageCache sharedImageCache] GetImage:imageName];
}
else if([[ImageCache sharedImageCache] DoesExist:imageName] == NO)
{
// for adding into cache
[[ImageCache sharedImageCache] AddImage:imageName :image]
//here image name is key , and image is object (UIImage ) value
// Here we we use key value pairs for saving data in cache
}
you have to create the methods used here in cache's class like this: 您必须像这样在缓存的类中创建此处使用的方法:
- (void) AddImage:(NSString *)imageName :(UIImage *)image
{
[imgCache setObject:image forKey:imageName];
}
- (NSString*) GetImage:(NSString *)imageName
{
return [imgCache objectForKey:imageName];
} }
- (BOOL) DoesExist:(NSString *)imageName
{
if ([imgCache objectForKey:imageName] == nil)
{
return false ;
}
return true;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.