我正在尝试在项目中使用Moya Networking。我正在使用此example。该示例是在下面的第56行上发出moya请求连接到视图控制器中的服务器,并在第72行和第78行中使用回调方法
func uploadGiphy() {
provider.request(MultiTarget(Giphy.upload(gif: Giphy.animatedBirdData)),
callbackQueue: DispatchQueue.main,
progress: progressClosure,
completion: progressCompletionClosure)
}
我想在 NetworkHelper.swift 中而不是在我的视图控制器中编写此函数,但是,在我的视图控制器中使用其回调方法(第72行和第78行)。
所以我在 NetworkHelper 中编写了该函数:
class NetworkHelper{
static func getUsers(amount:Int=2,
gender:Gender = .Male,
success successCallback: @escaping ([UserModelMain]) -> Void,
error errorCallback: @escaping (Swift.Error) -> Void,
failure failureCallback: @escaping (Moya.MoyaError) -> Void,
progress progressClosure: ProgressBlock?) {
userProvider.request(.getUsers(amount: amount, gender: gender),
callbackQueue: DispatchQueue.main,
progress: progressClosure) { (result) in
switch result
{
case .success(let response):
if response.statusCode >= 200 && response.statusCode <= 300 {
guard let getResponseObject = try? response.map(to: [UserModelMain.self])else{
let error = NSError(domain:"com.company.networkLayer", code:0, userInfo:[NSLocalizedDescriptionKey: "JSON Parsing Error"])
errorCallback(error)
return
}
successCallback(getResponseObject)
} else {
let error = NSError(domain:"com.company.networkLayer", code:0, userInfo:[NSLocalizedDescriptionKey: "Network Error"])
errorCallback(error)
}
case .failure(let error):
// 3:
failureCallback(error)
}
}
}
}
在我的视图控制器中,我正在调用此函数:
func user(){
UserAdapter.getUsers(amount: 200, gender: .Female, success: { (response) in
}, error: { (error) in
// show error from server
}, failure: { (error) in
// show Moya error
}, progress: progressClosure)
}
lazy var progressClosure: ProgressBlock = { response in
print("Progress: \(response.progress)")
}
但是response.progress
始终是0.0
的帮助。
进度:0.0进度:0.0进度:0.0进度:0.0进度:0.0