如果我取消注释下面的注释代码,我可以使用以下代码。如果我评论它,它不起作用。我不知道问题是什么:
func getMostPopularRepositories(byLanguage language: String) -> Observable<[Repository]> {
let encodedLanguage = language.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let provider = MoyaProvider<Github>(plugins: [NetworkLoggerPlugin(verbose: true)])
let parameters = [“q”: “language:\(encodedLanguage)“,“sort”: “stars”]
/*
provider.rx
.request(.repositories(parameters))
.map([Repository].self, atKeyPath: “items”)
.subscribe { event in
}.dispose()
*/
let request = provider.rx.request(.repositories(parameters)).asObservable()
// RxSwift.(unknown context at $101ccc3e8).AnonymousObservable<Moya.Response>
print(request)
let pRepos = request.map([Repository].self, atKeyPath: “items”)
// RxSwift.(unknown context at $101ccf26c).FlatMap<Moya.Response, RxSwift.Observable<Swift.Array<RepoSearcher.Repository>>>
print(pRepos)
return pRepos
}
我还没有和qazxsw poi合作过。
但似乎你需要Moya
使.subscribe
工作。
您需要订阅pRepos observable以发送网络请求,因为observable等待,直到观察者在开始发出项目之前订阅它,并在调用subscribe时开始向观察者推送值。
所以在你的情况下
.request
//订阅Observable <[Repository]>的代码
func getMostPopularRepositories(byLanguage language: String) -> Observable<[Repository]> {
let encodedLanguage = language.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let provider = MoyaProvider<Github>(plugins: [NetworkLoggerPlugin(verbose: true)])
let parameters = [“q”: “language:\(encodedLanguage)“,“sort”: “stars”]
let request = provider.rx.request(.repositories(parameters)).asObservable()
// RxSwift.(unknown context at $101ccc3e8).AnonymousObservable<Moya.Response>
print(request)
let pRepos = request.map([Repository].self, atKeyPath: “items”)
// RxSwift.(unknown context at $101ccf26c).FlatMap<Moya.Response, RxSwift.Observable<Swift.Array<RepoSearcher.Repository>>>
print(pRepos)
return pRepos
}