hyperoslo / Cache

:package: Nothing but Cache.
Other
2.96k stars 335 forks source link

Only one key observer permitted at a time? #284

Closed bryan-vh closed 3 years ago

bryan-vh commented 3 years ago

Are we only allowed to have one observer for a key at a time? If I update a value in one of my VC's that then updates the observer in that VC but not in another VC that also has an observer for the same key. Is there a way to get multiple observers on a single key?

Thanks!

3lvis commented 3 years ago

Hi @bryan-vh, I'm not sure, but I think that's something related to Foundation.

Not sure if this is useful but was the first thing I found on the internet. https://blog.pendowski.com/handling-multiple-observers-in-swift-2/

bryan-vh commented 3 years ago

Hi @3lvis, I ended up creating my own custom implementation that mirrors what the library does for key observers right now. As it stands, I believe that we can only have one observer for each key, since the map being used is [Key: (HybridStorage, KeyChange) -> Void]. In my implementation I changed this to [Key: [UUID: (Int) -> Void]], since my custom implementation is only for a counter that deals in Ints. Not sure if that's a good approach, but seems to work for me right now.

3lvis commented 3 years ago

Seems like a good and intuitive approach. This library is not in further development, makes sense for you to maintain your own implementation.

Have a great weekend :)