1

由于 NSCache 是一个黑盒子,如下所示:

1) 对象因内存压力而被移除时不发送系统通知。

2) 缓存不是由操作系统整体清除,而是在达到内存阈值后逐个对象减少。

有没有更好的方法来检查缓存对象是否仍然存在,而不是像这样检查它是否为空:

NSArray *array = [[MyCache sharedCache] someArray];
if (array.count > 0)
{
  //you're ok
}
else
{
  [self repopulateCache];
}
4

2 回答 2

2

MyCache 是您创建的类吗?使用 NSCache 你可以objectForKey用来检查是否还有东西。

于 2014-02-03T00:56:33.163 回答
1

利用 :

 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

    }

您必须像这样在缓存的类中创建此处使用的方法:

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

}

于 2014-02-03T07:00:08.037 回答