I need to cache json data from API in swift. So I researched a Lot & get to this Post.
I tried to implement the Option 1 in my App. But the Custom manager always returned nil. I don't know why?
After that I got AwesomeCache. It says that it an do Awesome API Caching. But I don't know how to implement this? I referred this Issue. Still I can't figure it Out.
This is how my Current implementation Looks without Cache:
Alamofire.request(.GET, "http://api.androidhive.info/volley/person_array.json")
.responseJSON { (_, _, data, _) in
let json = JSON(data!)
let catCount = json.count
for index in 0...catCount-1 {
let name = json[index]["name"].string
println(name)
}
Please suggest me the Best way to Cache JSON from API ?
Thanks in Advance!
UPDATE
These are my requirements
Fetch the JSON from the API & Parse the JSON data. These can be done with the help of Alamofire & SwiftyJSON
I will populate the parsed data in the Table View. It works when the user is in Online.
But I want to show the data in the Table when the user is in offline too.
So I need to save the Parsed data or the JSON data in my cache & I need to refresh or expire the cache within a week or days.
I don't prefer to store the JSON in my disk because it will be updated.
Please suggest me the Best way to achieve this...
You can use this cache open source. It cache data on disk and memory. Can cache many swift type, and custom class which inherit NSObject and conform NSCoding protocol.
https://github.com/huynguyencong/DataCache
To implement: First, it use NSCache for mem cache. NSCache use like a dictionary. Second, save cache to disk, use NSFileManager methods.
You have many tools already at your disposal.
NSURLCache
All your requests are already stored in the
NSURLCache
in theNSURLSessionConfiguration
on theNSURLSession
stored inside thesharedInstance
of the AlamofireManager
. Those stored requests already follow all the caching policy rules provided by the servers you are hitting. You can control the caching behavior by setting therequestCachePolicy
on your own custom NSURLSessionConfiguration. I'd also suggest you read through this awesome NSHipster article that walks you through the ins and outs of NSURLCache and how to control it.Downloading JSON to Disk
You can also download the JSON directly to disk using
Alamofire.download
instead of usingAlamofire.request
. This will download the payload to afileURL
that you provide in thedestination
closure. This would give you full control over the caching of the file after that point. You would need to create your own caching policy around these files afterwards if you wanted to follow the caching header rules provided by the server.Populating Table View
Once you have your data downloaded to disk, you need to load it into an
NSData
blob and parse it into JSON to populate your table view. This should be pretty straight forward. You need the destinationNSURL
that you specified to Alamofire when you started your download. Then load the file data into an NSData blob. Finally, use NSJSONSerialization to convert theNSData
object into a JSONAnyObject
which can be parsed into model objects to populate your table view.Storing JSON for Offline Usage
If you stick with this approach, you'll need to track your cache expiration dates in something like CoreData or SQLite. You can do this by either caching the paths to the JSON files on disk, or store the model objects directly in CoreData or SQLite. This could get fairly complicated and I would not recommend this approach unless you absolutely don't want to cache your model objects.
Offline Usage
Generally, if you need to cache data for offline usage, you want to store your model objects in something like CoreData. You would use the Alamofire
request
method coupled with aresponseJSON
serializer to parse the data into JSON. Then you would convert the JSON into model objects. From there, you'd save your model objects in CoreData, then finally populate your table view with the model objects.The nice thing about this approach is that you have all your model objects cached in the case that your table view is accessed when the device is offline. Coupling this design with queries to your
NSURLCache
to see if your request is cached let's you avoid unnecessary server calls and parsing logic when you already have your model objects generated.