【问题标题】:RxSwift: code working only first timeRxSwift:代码只在第一次工作
【发布时间】:2016-07-06 17:56:45
【问题描述】:

我是 RxSwift 的新手。我的代码中发生了一些奇怪的事情。 我有一个集合视图和

驱动程序[“字符串”]

绑定数据。

var items = fetchImages("flower")   
 items.asObservable().bindTo(self.collView.rx_itemsWithCellIdentifier("cell", cellType: ImageViewCell.self)) { (row, element, cell) in
           cell.imageView.setURL(NSURL(string: element), placeholderImage: UIImage(named: ""))           
}.addDisposableTo(self.disposeBag)

获取图片

函数返回数据

private func fetchImages(string:String) -> Driver<[String]> {

        let searchData = Observable.just(string)
        return searchData.observeOn(ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .Background))
            .flatMap
            { text in // .Background thread, network request

                return RxAlamofire
                    .requestJSON(.GET, "https://pixabay.com/api/?key=2557096-723b632d4f027a1a50018f846&q=\(text)&image_type=photo")
                    .debug()
                    .catchError { error in
                        print("aaaa")
                        return Observable.never()
                }
            }
            .map { (response, json) -> [String] in // again back to .Background, map objects
                var arr = [String]()
                for  i in 0 ..< json["hits"]!!.count {
                     arr.append(json["hits"]!![i]["previewURL"]!! as! String)
                }

                return arr
            }
            .observeOn(MainScheduler.instance) // switch to MainScheduler, UI updates
            .doOnError({ (type) in
                print(type)
            })
            .asDriver(onErrorJustReturn: []) // This also makes sure that we are on MainScheduler
    }

奇怪的是这个。第一次使用“flower”获取时它可以工作并返回数据,但是当我添加此代码时

self.searchBar.rx_text.subscribeNext { text in
      items = self.fetchImages(text)
}.addDisposableTo(self.disposeBag)

它不起作用。它不会在 flatmap 回调中执行步骤,因此不会返回任何内容。

【问题讨论】:

    标签: ios swift rx-swift


    【解决方案1】:

    它适用于您的第一个用例,因为您实际上是通过bindTo() 使用返回的Driver&lt;[String]&gt;

    var items = fetchImages("flower")
    items.asObservable().bindTo(...
    

    但是,在您的第二个用例中,您没有对返回的 Driver&lt;[String]&gt; 做任何事情,只是将其保存到一个变量中,而您对此什么都不做。

    items = self.fetchImages(text)
    

    Driver 直到您 subscribe(或者在您的情况下为 bindTo)之前什么都不做。

    编辑:为了使这一点更清楚,这里是你如何让你的第二个用例工作(我避免清理实现以保持简单):

    self.searchBar.rx_text
    .flatMap { searchText in
        return self.fetchImages(searchText)
    }
    .bindTo(self.collView.rx_itemsWithCellIdentifier("cell", cellType: ImageViewCell.self)) { (row, element, cell) in
        cell.imageView.setURL(NSURL(string: element), placeholderImage: UIImage(named: ""))           
    }.addDisposableTo(self.disposeBag)
    

    【讨论】:

    • 是的,我同意,但问题是,第二次 self.fetchImages(text) 没有返回任何东西,RxAlamofire .requestJSON 没有调用。它接缝平面图回调没有介入,因为那个电话不叫
    • “第二次”是什么意思?您已经展示了fetchImages 的两种不同用途。你的意思是你同时使用了这两种方法,如果它稍后出现,第二种方法不起作用?还是您的意思是您只使用了最后一个用例并且它确实有效,但只有一次?
    • 我两者都用,第一次我用“花”调用函数及其返回数据,第二次我使用搜索栏中的文本,它不返回任何内容。
    • 是的,所以我的观点仍然成立。当您在rx_text ObservablesubscribeNext 时,您只是在调用fetchImages 并获得一个new Driver,但您并没有对它做任何事情。除非你 subscribebindTo 它,否则它什么都不做。您可能会认为Observable.just(...) 会使其立即启动,但事实并非如此。它正等着你subscribebindTo 它。它具有justflatMap 的所有潜力,但直到你subscribebindTo 它才会出现。
    • 我已经编辑了我的答案,向您展示了一种通过最小更改使其工作的方法。仍有改进的余地,但它们对问题来说并不是必不可少的,而且会使答案变得不那么清晰。
    猜你喜欢
    • 2013-09-26
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多