3 Star 5 Fork 1

MQZHot / DaisyNet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

DaisyNet

AlamofireCache的封装实现对网络数据的缓存,可以存储JSON,String,Data.

使用

注意: 如果你的参数中带有时间戳、token等变化的参数,这些参数需要写在dynamicParams参数中,避免无法读取缓存

func request(
    _ url: String,
    method: HTTPMethod = .get,
    params: Parameters? = nil,
    dynamicParams: Parameters? = nil,
    encoding: ParameterEncoding = URLEncoding.default,
    headers: HTTPHeaders? = nil)
    -> RequestTaskManager
  • 缓存数据只需要调用.cache(true),不调用或者.cache(false)则不缓存
  • 调用responseCacheAndString可以先读取缓存数据,再读取网络数据
  • 通过isCacheData属性可以区分缓存数据还是网络数据
DaisyNet.request(url, params: params).cache(true).responseCacheAndJson { value in
    switch value.result {
    case .success(let json):
        if value.isCacheData {
            print("我是缓存的")
        } else {
            print("我是网络的")
        }
    case .failure(let error):
        print(error)
    }
}
  • 你也可以分别读取缓存数据和网络数据,如下代码
  • 调用cacheJson方法获取缓存数据,调用responseJson获取网络数据
DaisyNet.request(url, params: params).cache(true).cacheJson { json in
        print("我是缓存的")
    }.responseJson { response in
    print("我是网络的")
}
  • 如果你不需要缓存,可以直接调用responseJson方法
DaisyNet.request(url).responseString { response in
    switch response {
    case .success(let value): print(value)
    case .failure(let error): print(error)
    }
}
  • 同理,如果你要缓存Data或者String,与JSON是相似的
/// 先读取缓存,再读取网络数据
DaisyNet.request(url).cache(true).responseCacheAndString { value in }
DaisyNet.request(url).cache(true).responseCacheAndData { value in }
/// 分别获取缓存和网络数据
DaisyNet.request(url).cache(true).cacheString { string in
        print("我是缓存的")
    }.responseString { response in
    print("我是网络的")
}
  • 取消请求
DaisyNet.cancel(url, params: params)
  • 清除缓存
/// 清除所有缓存
func removeAllCache(completion: @escaping (Bool)->())
/// 根据url和params清除缓存
func removeObjectCache(_ url: String, params: [String: Any]? = nil, completion: @escaping (Bool)->())

Install

1.pod 'DaisyNet'

2.pod install / pod update

Author

LICENSE

DaisyNet is released under the MIT license. See LICENSE for details.

MIT License Copyright (c) 2017 MQZHot (https://github.com/MQZHot/DaisyNet) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

对Alamofire与Cache的封装实现对网络数据的缓存,可以存储JSON,String,Data 展开 收起
Swift 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Swift
1
https://gitee.com/MQZHot/DaisyNet.git
git@gitee.com:MQZHot/DaisyNet.git
MQZHot
DaisyNet
DaisyNet
master

搜索帮助

14c37bed 8189591 565d56ea 8189591