How to flush Objective C method's cache

Greetings,


It seems that whenever -[NSObject setValue:forKey:] is called for the first time runtime determines an accessor implementation that should be used and caches it for further KVC calls for the specified key. This cache is not flushed even when said accessor is swizzled, which leads to some undesirable behavior.

Is there a way to flush this cache or prevent KVC from using it, i.e. force KVC to lookup method every time?

Replies

The problem you face is that there's no API contract about this, so you can't exactly claim it isn't behaving as documented. Still, you could try a bug report, on the basis that what you're doing is based on a reasonable interpretation of the underlying mechanism.


Your description is a bit puzzling, though. If something is being cached, it can't be "an accessor implementation" for "the specified key", because it has to at least be class-specific. That suggests you should get your swizzled implementation if you replace the standard implementation before "setValue:forKey:" is first called for the class and key. While I can imagine general scenarios where you might swizzle accessors for classes you don't "own" after the original accessors have already been used, they're pretty scary. (For example, you have no control over whether the class uses accessors consistently, or uses a mixture of accessor methods and instance variables.)

Thanks for the reply, Quincey.


Yes, I'm swizzling methods on classes I don't own and the posible inconsistency you were talking about is the risk I'm willing to take.

I guess due to missing API to work with the methods cache, the only solution for my problem would be to swizzle implementation as early as possible.