This NSCache subclass tries to cache data in memory.
- Xcode 8
- Swift 3
- Simply drag and drop the "WARCache.swift" in your project.
WARCache.shared.fech(url: searchURL) { (data, error) in
// cached data or new data from server
}
Example included with the project
- Search with same string more than once. The first time it will take more time as it fetch from server.
- To test same image requested by multiple sources simultaneously (even before it has loaded) uncomment line 100 of
Flickr.swift
- Logs on debuger console will also help
Reason for NSCache subclass: NSCache is basically just an NSMutableDictionary that automatically evicts objects in order to free up space in memory as needed. Source: NSCache
Set number of concurrent downloads (Not implemented)
HTTPMaximumConnectionsPerHost
has some limitation. see Set number of concurrent downloads also How do I use NSOperationQueue with NSURLSession
There is a popular open source: HanekeSwift
Initial Project taken from UICollectionView Tutorial: Getting Started and then modified Flickr.swift
and added WARCache.swift
remaining files are untouched