gpt4 book ai didi

swift3 - RxAlamofire发布多部分数据

转载 作者:行者123 更新时间:2023-12-04 12:18:57 28 4
gpt4 key购买 nike

如何使用 RxAlamofire 发送多部分数据

For instance in Alamofire

let URL = try! URLRequest(url: "http://example.com", method: .post)


Alamofire.upload(multipartFormData: { formData in
// multiaprt
}, with: URL, encodingCompletion: {(result: SessionManager.MultipartFormDataEncodingResult) in

})
  1. 如何获得与 RxAlamofire 相同的行为/功能?
  2. 或者将此函数包装在 Observable 中的方法?

最佳答案

您可以像这样轻松地包装该函数:

func wrapper() -> Observable<SomeResponseType> {
return Observable.create { observer in

let URL = try! URLRequest(url: "http://example.com", method: .post)

Alamofire.upload(
multipartFormData: { formData in
// multiaprt
},
with: URL,
encodingCompletion: { encodingResult in

switch encodingResult {

case .success(let upload, _, _):
upload.responseJSON { response in
// convert response in something of SomeResponseType
// ...
observer.onNext(response)
observer.onCompleted()
}
case .failure(let encodingError):
observer.onError(encodingError)
}
})

return Disposables.create()
}
}

关于swift3 - RxAlamofire发布多部分数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41360676/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com