我需要在swift中缓存来自API的json数据.所以我研究了很多并获得了这篇文章.
我试图在我的应用程序中实现选项1.但是自定义管理器总是返回零.我不知道为什么?
之后我得到了AwesomeCache.它说这是一个很棒的API缓存.但我不知道如何实现这个?我提到了这个问题.我仍然想不通.
这是我的Current实现在没有缓存的情况下的外观:
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)
}
Run Code Online (Sandbox Code Playgroud)
请建议我从API缓存JSON的最佳方法?
提前致谢!
UPDATE
这些是我的要求
从API获取JSON并解析JSON数据.这些可以在Alamofire和SwiftyJSON的帮助下完成
我将在表视图中填充解析的数据.它适用于用户在线时.
但我想在用户处于离线状态时显示表中的数据.
所以我需要在我的缓存中保存解析数据或JSON数据,我需要在一周或几天内刷新或过期缓存.
我不喜欢将JSON存储在我的磁盘中,因为它会被更新.
请建议我实现这个目标的最好方法......
我正在尝试将URLRequest中的数据/响应注入我的缓存中的另一个 URLRequest.
这只是一个示例代码.它已准备好被转储到一个项目中.
我正在尝试做的是使用从我的landscapeURLString网络请求中检索到的响应+数据...存储到我的会话缓存中以获取我的lizardURLString请求.
import UIKit
class ViewController: UIViewController {
lazy var defaultSession : URLSession = {
let urlCache = URLCache(memoryCapacity: 500 * 1024 * 1024, diskCapacity: 500 * 1024 * 1024, diskPath: "something")
let configuration = URLSessionConfiguration.default
configuration.urlCache = urlCache
let session = URLSession(configuration: configuration)
return session
}()
lazy var downloadLizzardbutton : UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("download lizard image OFFLINE", for: .normal)
btn.backgroundColor = .blue
btn.addTarget(self, …Run Code Online (Sandbox Code Playgroud)