I need to make a dictionary like NSCache which is thread-safe for keeping a cahce. However, once in a while, I would need to refresh the contents of the Cache. NSCache does not provide a method to iterate over its keys. What would be the alternative here ? NSMutableDictionary with synchronization ?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
- Why is my library not able to expand on the CocoaP
NSCache doesn't have such API. So, you should implement your own cache using NSMutaleDictionary or NSMapTable. The main idea is to track order of added objects and remove the older if countLimit is exceeded. This can be done using some ordered structure for storing keys in order of adding, e.g. NSMutableArray. Also, you should track memory warnings and cleanup you cache.
For thread-safety you can use any lock that you like (@synchronized, NSLock, etc.). And you definitely should take keyEnumerator and objectEnumerator from copy of your mutable storage.
You can look at thread-safe implementation with ability to iterate through keys and objects: VSCache