繁体   English   中英

RxSwift。 订阅发生两次

[英]RxSwift. Subscription occurs twice

我不太了解 RxSwift,也无法自己解决我的问题。 我将非常感谢您的帮助。 对不起我的英语不好。

//declare variables    

var loadedImage = BehaviorRelay<UIImage?>(value: nil)

//Subscribe when creating a cell

loadedImage
            .do(onNext: { (_) in
                cell.activityIndicator.startAnimating()
                cell.activityIndicator.isHidden = false
            })
            .flatMap { (image) -> Observable<String> in
                guard let image = image else { return Observable.just("") }
                return service.uploadPhoto(image: image)
            }
            .observeOn(MainScheduler.instance)
            .subscribeNext { (imageName) in
                vc.createEventData.imageName = imageName

                cell.activityIndicator.stopAnimating()
                cell.activityIndicator.isHidden = true

                let currentPhoto = self.object?.placeholderPhoto
                let image = self.loadedImage.value

                cell.photoEventImageView.image = image == nil ? currentPhoto : image

                if let photoPath = self.object?.photoPath, image == nil {
                    if photoPath != "uploads/photos/.png" {
                        let url = URL(string: "http://test-around.profsoft.online/" + photoPath)
                        cell.photoEventImageView.kf.setImage(with: url)

                        if let r = photoPath.range(of: "/", options: .backwards) {
                            let imageName = photoPath.substring(from: r.upperBound)
                            vc.createEventData.imageName = imageName
                        }
                    }
                }

            }
            .disposed(by: disposeBag)

//I throw a signal when choosing a photo

   func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        guard let image = info[.originalImage] as? UIImage else { return }
        loadedImage.accept(image)
        self.viewController?.dismiss(animated: true, completion: nil)
    }

我不明白为什么订阅会发生 2 次。 也就是我go到service.uploadPhoto(image:image)两次。

我只能根据您发布的代码在这里推测,但当单元格被重用时您可能不会取消订阅。 因此,由于 disposeBag 在调用cellForRowAtIndexPath时由 ViewController 持有,因此您只需添加另一个订阅。 只有在 ViewController 被释放后,订阅才会被清除,也就是 DisposeBag 被销毁的时候。

这是我现在要做的:单元格应该包含 Observable 和 DisposeBag 。 您必须在prepareForReuse中清除它:

class YourCell: UITableViewCell {
   var disposeBag = DisposeBag()
   var yourObservable: Observable<UIImage?>?

   func configure(with yourObservable: Observable<UIImage?>) {
      yourObservable = yourObservable
         .subscribe(onNext: /*your code*/ }
         .disposed(by: disposeBag)
   }

   override func prepareForReuse() {
      super.prepareForReuse()
      disposeBag = DisposeBag()
   }
}

然后在你的 ViewController 中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let observable = loadedImage /* additional config if needed */
   let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR IDENTIFIER HERE", for: indexPath) as! YourCell
   cell.configure(with: observable)
   return cell
}

此时,您可能还想考虑您的架构。 例如,您可以管理为单元格提供数据的可观察对象,以便您可以将其绑定到 tableview 并让 rx 管理所有内容。 或者,您可以覆盖 TableViewDataSource,使您的单元格可以完全自己处理自己的 ViewModel。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM