可以按需清除未使用对象的缓存 [英] Cache that can purge unused objects by demand

查看:69
本文介绍了可以按需清除未使用对象的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个可以保存一些对象的缓存,但是,当我有内存警告或只是用户想要它时,我想清除仅使用 的所有实例. >目前处于缓存中.

I need to create a cache, that could save some objects, but then, at some point when I have memory warning or simply user wants it, I would like to purge all instances that are used only in the cache at the moment.

换句话说:我需要使用ARC count == 1来取消初始化对象.问题是,基于我对该项目的谷歌搜索,在纯Swift中不可能获得对象的保留计数.

In other words: I need to deinit objects with ARC count == 1. The problem is that based on my googling of this project, it is not possible in pure Swift to get the retain count of an object.

根据我的经验,我发现默认情况下在Swift中是不可能的.在Objective-C中,我使用的是Proxy对象,该对象是从缓存返回的,它具有这样的覆盖方法:

From my experience I see that it is not possible by default in Swift. In objective-c I was using Proxy object, that was returned from cache, it had such overriden methods:

    // Forward class checks for assertions.
-(BOOL)isKindOfClass:(Class)aClass {return [_target isKindOfClass:aClass];}

- (id)forwardingTargetForSelector:(SEL)aSelector
{
    return(_target);
}

但是当然不适用于Swift意识形态.

But is of course not applicable to Swift ideology.

我以为我的缓存是基于WeakBoxes数组,但是这样,当未使用的对象变为未使用且不符合我的要求时,它们将被释放.

One thought that I have is to base my cache on array of WeakBoxes, but this way unused objects will be deallocated when they become unused and that doesnt meet my requirements.

有人可以指导我一些我不知道的Swift可能性来实现这种目标吗?

Can somebody guide me to some Swift possibilities, that I'm not aware of, to achieve such thing?

推荐答案

您无需弄乱对象的保留计数.您的缓存可以拥有很强的参考力.这样可以保证保留数始终至少为1.当收到内存警告时,您只需遍历缓存中的每个指针并将其设置为nil.假设没有其他人拥有强引用,则这会将引用计数减为零,并且对象立即调用deinit并耗尽内存.如果您确实真的希望对象在缓存进行清除时耗尽内存,请确保仅缓存对所保存的项目有强引用,而其他所有人都没有引用.我有一个懒惰的ViewControllers数组,它执行类似的操作:

You do not need to mess with the retain count of the object. Your cache can just hold a strong reference. This guarantees that the retain count is always at least one. When you get a memory warning you just loop through every pointer in the cache and set it to nil. Assuming no one else has a strong reference this decrements the reference count to zero and the object immediately calls deinit and goes out of memory. If you really really want the object to go out of memory when the cache does a purge, make sure only the cache has a strong reference to the items being held and that everyone else takes a weak reference. I have a lazily loaded array of ViewControllers that does something similar:

    fileprivate var controllers = [UIViewController?](repeating: nil, count:  DashboardInfo.Category.allValues.count)

    override func didReceiveMemoryWarning() {
        //Release every off screen controller
        for index in 0 ..< controllers.count {
            if controllers[index] != currentController {
                controllers[index]?.removeFromParentViewController()
                controllers[index]?.view.removeFromSuperview()
                controllers[index] = nil
            }
        }
    }

这篇关于可以按需清除未使用对象的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆