美文网首页
iOS开发swift -- Alamofire数据缓存

iOS开发swift -- Alamofire数据缓存

作者: Hayley__ | 来源:发表于2017-06-01 17:27 被阅读1289次

    一 初始设置

            //appdelegate中 创建缓存
            NetworkReachabilityManager.init()?.startListening()
            let urlCache = URLCache.init(memoryCapacity: 4*1024*1024, diskCapacity: 20*1024*1024, diskPath: nil)
            URLCache.shared = urlCache
    

    二 网络请求

            let manager: SessionManager = {
                let configuration = URLSessionConfiguration.default
                let reachability = NetworkReachabilityManager()
                if (reachability?.isReachable)! {
                    configuration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
                }else
                {
                    configuration.requestCachePolicy = .returnCacheDataElseLoad
                }
                return Alamofire.SessionManager(configuration: configuration)
            }()
        
            manager.request("http://192.168.1.133:8080/b------/squareList",method : .post ,parameters :["begin": page,"token": currentUser.token]).responseJSON { (response) in
                let dataResponse = JSON(response.data!)
                let data = dataResponse["data"]
                completion(true, data.arrayValue.count < perPage, data.arrayValue.map({ Wish(json: $0) }))
                manager.session.invalidateAndCancel()
            }
    

    先简单记录 后续会细化:)

    相关文章

      网友评论

          本文标题:iOS开发swift -- Alamofire数据缓存

          本文链接:https://www.haomeiwen.com/subject/ulmefxtx.html