Question

Since NSCache is a black box as in:

1) No system notification is sent when objects are removed due to memory pressure.

2) The cache is not purged by the OS en total but pared down object-by-object after a memory threshold is reached.

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];
}
Was it helpful?

Solution

Is MyCache a class you created? With NSCache you could use objectForKey to check if something is still there.

OTHER TIPS

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;

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top