Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8893339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:13:20+00:00 2026-06-14T23:13:20+00:00

I want to get notified when the object corresponding to a certain key in

  • 0

I want to get notified when the object corresponding to a certain key in an NSCache instance changes. I’ve tried using Key-Value Observing without luck (the console doesn’t log anything).

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.cache addObserver:self forKeyPath:@"myKey" options:NSKeyValueObservingOptionNew context:NULL];
    [self.cache setObject:@"myObject" forKey:@"myKey"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"KEYPATH CHANGED: %@", keyPath);
}

How can I observe the contents of an NSCache?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:13:22+00:00Added an answer on June 14, 2026 at 11:13 pm

    I would expect NSCache not to be key-value observing (KVO) compliant for that sort of key. KVO is implemented internally at the NSObject level by replacing the normal setter with one that alerts relevant observers and calls the original setter. In the case of things you set with setObject:forKey: there is no specific setter so nothing for the runtime to hang off.

    Since NSCache doesn’t post any relevant notifications I think your best hope is the delegate protocol. It’s not explicit exactly what counts as an eviction but if cache:willEvictObject: is called when the object associated with a key is changed on purpose then you could hook directly off that.

    Otherwise I’d recommend you create a wrapper class for NSCache, say DWCache for argument’s sake, that contains an NSCache, is the delegate of the cache, and provides its own setObject:forKey:. It will posts an appropriate message (i) upon the first call to setObject:forKey:; (ii) upon every subsequent call that supplies an object different from that already in the cache; and (iii) whenever it receives a cache:willEvictObject:.

    The only potential complexity is that NSCache doesn’t copy the original keys and, partly as a result, has no means to get key from object. You may want to store those connections separately, e.g. through an NSDictionary. Provided you clear the dictionary when the cache evicts the object you’ll still be providing caching behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to watch a system setting and get notified when its value changes.
I want get all of the Geom objects that are related to a certain
I have an object that has a list of 'observers'. These observers get notified
I want to get notified when a new lead is created in Salesforce. Is
I want to get notified when an item in a ListBox gets clicked by
I want to get notified about all the events happening in a web page.
Short version: For a cache class I need to get notified if an object
Having used the Facebook commenting plugin on my site, I want to get notified
I want to get notified when I click a window in WPF (I use
from a ff 3.6 plugin I want to get notified every time a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.