创建的可观察对象不发出任何东西
Created observable doesn't emit anything
我正在尝试创建一个 returns Observable<(HTTPURLResponse, Any)>
使用 RxAlamofire
:
的函数
class APIManager {
let disposeBag = DisposeBag()
func getResponse(credentialData: Credentials, ulr: String)->Observable<(HTTPURLResponse, Any)>{
let credentialData = "\(credentialData.username):\(credentialData.password)".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let base64Credentials = credentialData.base64EncodedString()
let headers = ("Authorization", "Basic \(base64Credentials)")
let header = HTTPHeaders.init(dictionaryLiteral: headers)
return Observable.create{ observer in
requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
.subscribe(onNext: { response in
observer.onNext(response)
} ,
onError: { error in
observer.onError(error)
},
onCompleted: {},
onDisposed: {}).disposed(by: self.disposeBag)
return Disposables.create()
}
}
}
进入onNext
的代码没有执行;它仅通过 onDisposed
。
我该如何解决?
为什么你用另一个 Observable 包装 requestJSON
函数,它只是转发相同的元素和错误,而那个函数已经返回你需要的 Observable?
这还不够吗?
class APIManager {
func getResponse(credentialData: Credentials, ulr: String) -> Observable<(HTTPURLResponse, Any)> {
let credentialData = "\(credentialData.username):\(credentialData.password)"
.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let base64Credentials = credentialData.base64EncodedString()
let headers = ("Authorization", "Basic \(base64Credentials)")
let header = HTTPHeaders.init(dictionaryLiteral: headers)
return requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
}
}
我正在尝试创建一个 returns Observable<(HTTPURLResponse, Any)>
使用 RxAlamofire
:
class APIManager {
let disposeBag = DisposeBag()
func getResponse(credentialData: Credentials, ulr: String)->Observable<(HTTPURLResponse, Any)>{
let credentialData = "\(credentialData.username):\(credentialData.password)".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let base64Credentials = credentialData.base64EncodedString()
let headers = ("Authorization", "Basic \(base64Credentials)")
let header = HTTPHeaders.init(dictionaryLiteral: headers)
return Observable.create{ observer in
requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
.subscribe(onNext: { response in
observer.onNext(response)
} ,
onError: { error in
observer.onError(error)
},
onCompleted: {},
onDisposed: {}).disposed(by: self.disposeBag)
return Disposables.create()
}
}
}
进入onNext
的代码没有执行;它仅通过 onDisposed
。
我该如何解决?
为什么你用另一个 Observable 包装 requestJSON
函数,它只是转发相同的元素和错误,而那个函数已经返回你需要的 Observable?
这还不够吗?
class APIManager {
func getResponse(credentialData: Credentials, ulr: String) -> Observable<(HTTPURLResponse, Any)> {
let credentialData = "\(credentialData.username):\(credentialData.password)"
.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let base64Credentials = credentialData.base64EncodedString()
let headers = ("Authorization", "Basic \(base64Credentials)")
let header = HTTPHeaders.init(dictionaryLiteral: headers)
return requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
}
}