我尝试创建一个可观察的请求。但我错过了一些东西。我不明白缺少了什么?
我做了和this doc, this, this, etc.
func performRequest<T: Codable>(route: BaseRequestProtocol, withCookie: Bool = true) -> Observable<T> {
let encoding: ParameterEncoding = route.method == .get ? URLEncoding.default : JSONEncoding.default
var headers: HTTPHeaders = [
"Accept": "application/json",
"Content-Type": "application/json; charset=utf-8"
]
if withCookie {
headers["Cookie"] = SessionManager.shared.getCookie()
}
print(headers)
print(route.path)
let task = self.beginBackgroundTask()
return Observable<T>.create { (observer) -> Disposable in
guard let currentSession = self.session else { return Disposables.create {
observer
} }
let request = currentSession.rx.request(route.method,
(URL(string: route.path)) ?? URLConvertible,
parameters: route.body,
encoding: encoding,
headers: headers)
.responseJSON()
.observe(on: MainScheduler.instance)
.subscribe { response in
self.endBackgroundTask(taskID: task)
switch response {
case .error(let error):
observer.onResponseError(error)
case .success(let result):
observer.onNext(result)
}
} onError: { error in
observer.onResponseError(error)
} onCompleted: {
observer.onCompleted()
}
return Disposables.create { request.cancel }
}
.asObservable() // <------ Type of expression is ambiguous without more context
}
需要一些挑剔的眼光来帮助我错过什么!回复 回复
谢谢!