CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

DaisyNet 1.1.2

DaisyNet 1.1.2

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2025
SPMSupports SPM

Maintained by mengqingzheng, Alucardulad.



 
Depends on:
Cache~> 6.0.0
Alamofire~> 5.10.1
 

DaisyNet 1.1.2

  • By
  • MQZHot

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.