RxSwift API响应

问题描述 投票:0回答:2

如果我取消注释下面的注释代码,我可以使用以下代码。如果我评论它,它不起作用。我不知道问题是什么:

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
}
swift rx-swift
2个回答
2
投票

我还没有和qazxsw poi合作过。

但似乎你需要Moya使.subscribe工作。


0
投票

您需要订阅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
}
© www.soinside.com 2019 - 2024. All rights reserved.