Moya 13.0.1
Swift 5.0
// 步骤一:实现自定义插件
class TestPlugin: Moya.PluginType {
func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) {
print("=====")
}
func willSend(_ request: RequestType, target: TargetType) {
print("=====")
}
}
// 步骤二:执行网络请求
let provider = MoyaProvider<TestAPI>.init(plugins: [TestPlugin()])
provider.request(TestAPI(baseURL: URL(string: "http:/xxxxxx.com")!), completion: { (result) in
print("====")
})
问题:
当发起网络请求之后,testPlugin的willSend会被调用,但是当接收到网络数据后,didReceive却没有被调用。而是调用了Moya.PluginType的extension中的默认实现?
public extension PluginType {
func prepare(_ request: URLRequest, target: TargetType) -> URLRequest { return request }
func willSend(_ request: RequestType, target: TargetType) { }
func didReceive(_ result: Result<Moya.Response, MoyaError>, target: TargetType) { }
func process(_ result: Result<Moya.Response, MoyaError>, target: TargetType) -> Result<Moya.Response, MoyaError> { return result }
}
_Sorry I'm replying in English_
This example seems to work fine:
class TestPlugin : PluginType {
func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) {
print("did receive")
}
func willSend(_ request: RequestType, target: TargetType) {
print("will send")
}
}
usage:
let provider = MoyaProvider<ExampleTarget>(plugins: [TestPlugin()])
provider.request(.example) { result in
switch result {
case .success(_):
print("SUCCESS")
case .failure(_):
print("FAILURE")
}
}
Output:
will send
did receive
SUCCESS
you need
import Moya
import Result
you need
import Moya import Result
thanks
need import Result. Has been resolved. thanks
thanks!
Most helpful comment
you need