使用Moya缓存处理

AMA*_*N77 2 swift alamofire moya

我们在项目中实施了Moya,RxSwift和Alamofire作为pods.

有谁知道如何使用这种技术控制每个网址请求的缓存策略?

我已经阅读了Moya的GitHub页面上的一些问题,但仍然找不到任何东西.还尝试使用存储为sampleData文件的实际json响应,如下所示:

var sampleData: Data {
    guard let path = Bundle.main.path(forResource: "SampleAggregate", ofType: "txt") else {
        return "sampleData".utf8Encoded
    }
    let sample = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)
    return sample!.utf8Encoded

}
Run Code Online (Sandbox Code Playgroud)

在此先感谢任何专业提示:)

fre*_*dpi 12

至于我的理解,解决这个问题的"最干净"的方法是使用自定义的Moya插件.这是一个实现:

protocol CachePolicyGettable {
    var cachePolicy: URLRequest.CachePolicy { get }
}  

final class CachePolicyPlugin: PluginType {
    public func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
        if let cachePolicyGettable = target as? CachePolicyGettable {
            var mutableRequest = request
            mutableRequest.cachePolicy = cachePolicyGettable.cachePolicy
            return mutableRequest
        }

        return request
    }
}
Run Code Online (Sandbox Code Playgroud)

要实际使用此插件,还需要执行两个步骤:

  1. 应该将插件添加到您的Moya提供程序中,如下所示:

    let moyaProvider = MoyaProvider<YourMoyaTarget>(plugins: [CachePolicyPlugin()])
    
    Run Code Online (Sandbox Code Playgroud)
  2. YourMoyaTarget应符合CachePolicyGettable并从而为每个目标定义缓存策略:

    extension YourMoyaTarget: CachePolicyGettable {
        var cachePolicy: URLRequest.CachePolicy {
            switch self {
            case .sampleTarget, .someOtherSampleTarget:
                return .reloadIgnoringLocalCacheData
    
            default:
                return .useProtocolCachePolicy
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

注意:此方法使用协议将缓存策略与目标类型相关联; 也可以通过传递给插件的闭包来实现这一点.然后,这种闭包将决定使用哪个高速缓存策略,这取决于作为闭包的输入参数传递的目标类型.


Tim*_*nin 2

子类化MoyaProvider并组合requestClosure

它应该看起来像:

final class MyProvider<Target: TargetType>: MoyaProvider<Target> {

    public init(
        endpointClosure: @escaping EndpointClosure = MoyaProvider.defaultEndpointMapping,
        requestClosure: @escaping RequestClosure = MoyaProvider.defaultRequestMapping,
        stubClosure: @escaping StubClosure = MoyaProvider.neverStub,
        manager: Manager = MoyaProvider<Target>.defaultAlamofireManager(),
        plugins: [PluginType] = [],
        trackInflights: Bool = false
    ) {
        super.init(
            endpointClosure: endpointClosure,
            requestClosure: { endpoint, closure in
                var request = try! endpoint.urlRequest() //Feel free to embed proper error handling
                if request.url == URL(string: "http://google.com")! {
                    request.cachePolicy = .returnCacheDataDontLoad
                } else {
                    request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
                }
                closure(.success(request))
            },
            stubClosure: stubClosure,
            manager: manager,
            plugins: plugins,
            trackInflights: trackInflights
        )
    }

}
Run Code Online (Sandbox Code Playgroud)