简体   繁体   English

检查对象是否已从NSCache中删除的最佳方法?

[英]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.

相关问题 有什么方法可以检查SKSpriteNode是否已从父级移除? - Is there any way to check if an SKSpriteNode has been removed from parent? NSUserDefaults或NSCache - 存储数据的最佳方式 - NSUserDefaults or NSCache - best way to store data 检查我的UIView是否已显示的最佳方法是什么? - What is the best way to check if my UIView has already been shown? 通知后端已禁用推送通知的最佳方法是什么? - What is the best way to notify backend that push notifications have been disabled? 在应用程序中最好地使用NSCache - Best usage of NSCache across an app 有没有一种方法可以查看哪些成就已被统一解锁? - Is there a way to check to see which achievements have been unlocked in unity? 在NSCache中存储struct的任何方法 - Any way to store struct in NSCache 有没有办法在NSCache中存储可选数组? - Is there a way to store an array of optionals in NSCache? 即使从XCode项目中删除了所有框架,iOS应用程序仍会生成 - iOS app builds even after all the frameworks have been removed from the XCode project iOS6上的Autolayout问题:“应该从引擎中删除所有依赖约束,也可以从视图的依赖约束列表中删除” - Autolayout issue on iOS6: “All dependent constraints should have been removed from the engine and also from the view's list of dependent constraints”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM