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.

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
  • 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. – miho Jan 24 '14 at 19:54
  • 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. – Steven Fisher Jan 24 '14 at 19:58

4 Answers4

10

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)")
    }
}
jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72
2

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

miho
  • 11,765
  • 7
  • 42
  • 85
proxi
  • 1,243
  • 10
  • 18
1

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

It's thread-safe and easy to use.

J. Doe
  • 114
  • 5
0

You can see all keys and objects with

-[NSCache mapTableRepresentation]

mackworth
  • 5,873
  • 2
  • 29
  • 49