2

我有一些具有委托方法的第三方库。但我喜欢使用 RX,所以我应该创建 RxDelegateProxy 来通过 RX 接收委托的回调。

这是我的自定义 DelegateProxy 类:

extension Reactive where Base:GalleryController {

  var selectedImages:Observable<(GalleryController, [Image])> {
    let proxy = RxGalleryDelegateProxy.createProxy(for: self.base)
    return proxy.imageSubject.asObservable()
  }

}

private class RxGalleryDelegateProxy: DelegateProxy<GalleryController, GalleryControllerDelegate>, DelegateProxyType, GalleryControllerDelegate {

  private var _imageSubject: PublishSubject<(GalleryController, [Image])>?

  public weak fileprivate(set) var galleryController: GalleryController?

  internal var imageSubject: PublishSubject<(GalleryController, [Image])> {
    if let subject = _imageSubject {
      return subject
    }

    let subject = PublishSubject<(GalleryController, [Image])>()
    _imageSubject = subject

    return subject
  }

  static func currentDelegate(for object: GalleryController) -> GalleryControllerDelegate? {
    return object.delegate
  }

  static func setCurrentDelegate(_ delegate: GalleryControllerDelegate?, to object: GalleryController) {
    object.delegate = delegate
  }

  static func registerKnownImplementations() {
    self.register { RxGalleryDelegateProxy(parentObject: $0) }
  }

  private init(parentObject: GalleryController) {
    self.galleryController = castOrFatalError(parentObject)
    super.init(parentObject: parentObject, delegateProxy: RxGalleryDelegateProxy.self)
  }

  func galleryController(_ controller: GalleryController, didSelectImages images: [Image]) {
    if let subject = _imageSubject {
      subject.on(.next((controller, images)))
    }

    self._setForwardToDelegate(galleryController(controller, didSelectImages: images), retainDelegate: true)
  }

  deinit {
    _imageSubject?.on(.completed)
  }

}

在我的 UIViewController 我订阅:

final class PhotoLibraryViewController: UIViewController {

  private let _bag = DisposeBag()

  override func viewDidLoad() {
    super.viewDidLoad()

    let gallery = GalleryController()
    present(gallery, animated: true, completion: nil)

    gallery.rx.selectedImages
      .debug("--------")
      .observeOn(MainScheduler.instance)
      .subscribe(onNext: { (controller, images) in
        print("\(images)")
      }, onError: { (error) in
        DDLogError("Error:\n\(error)")
      })
      .disposed(by: _bag)

  }

}

但是我通过 .debug("--------") 在控制台输出中得到的只是:

2018-01-09 20:05:14.814: -------- -> subscribed
2018-01-09 20:05:14.817: -------- -> Event completed
2018-01-09 20:05:14.817: -------- -> isDisposed

所以我的对象在创建后立即处理。我做错了什么?

4

1 回答 1

0

你的gallery对象是在里面创建的viewDidLoad(),所以一旦viewDidLoad()完成,deinit方法 fromgallery被调用并被释放。

将您的gallery对象移出viewDidLoad()

final class PhotoLibraryViewController: UIViewController {

  private let _bag = DisposeBag()
  let gallery = GalleryController()

  override func viewDidLoad() {
    super.viewDidLoad()

    present(gallery, animated: true, completion: nil)

    gallery.rx.selectedImages
      .debug("--------")
      .observeOn(MainScheduler.instance)
      .subscribe(onNext: { (controller, images) in
        print("\(images)")
      }, onError: { (error) in
        DDLogError("Error:\n\(error)")
      })
      .disposed(by: _bag)

  }

}
于 2018-01-31T12:07:38.573 回答