Inspect NSCache contents on iOS?
Asked Answered
T

4

7

Is there a way to inspect NSCache's contents for debugging purposes?

This would not be used in the App Store. I just would like to be able to see what's going on in there while debugging. The description is pretty useless.

Tribe answered 24/1, 2014 at 19:24 Comment(2)
I don't know any existing tool which can help you doing that. But NSCache is thread-safe. You may just want to write a small daemon which stores the content of NSCache to disk based on a timer.Kerenkeresan
While you can pull objects out of a cache based on their key, there doesn't seem to be a way to enumerate or get all the keys.Tribe
T
2

There is an open-source replacement for NSCache that solves this exact issue: https://github.com/gregkrsak/GKCache

Ticktack answered 24/1, 2014 at 21:56 Comment(1)
This is exactly the code I was about to write, so thanks for the link. :)Tribe
A
14

No need to use a third party library. You can do this using the "allObjects" key from NSCache. For example:

if let all = yourCache.value(forKey: "allObjects") as? NSArray {
    for object in all {
        print("object is \(object)")
    }
}
Alberthaalberti answered 8/4, 2019 at 9:33 Comment(0)
T
2

There is an open-source replacement for NSCache that solves this exact issue: https://github.com/gregkrsak/GKCache

Ticktack answered 24/1, 2014 at 21:56 Comment(1)
This is exactly the code I was about to write, so thanks for the link. :)Tribe
H
1

NSCache doesn't have such feature. But you can use custom solution, e.g.: VSCache

It's thread-safe and easy to use.

Hales answered 10/9, 2018 at 12:35 Comment(0)
P
1

You can see all keys and objects with

-[NSCache mapTableRepresentation]

Presidium answered 11/10, 2021 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.