【问题标题】:RxSwift Remove an element BehaviorSubjectRxSwift 移除一个元素 BehaviorSubject
【发布时间】:2020-01-22 22:07:46
【问题描述】:

我有一个 BehaviorSubject 数组。我想通过 id 从这个数组中删除一张照片。

我试过了;

photosViewModel.favoritePhotos
            .observeOn(MainScheduler.instance)
            .map { photoList in
                photoList.filter { $0.id! != self.currentPhoto.id! } //remove currentPhoto
        }.bind(to: photosViewModel.favoritePhotos)
        .disposed(by: disposeBag)

【问题讨论】:

    标签: swift rx-swift behaviorsubject


    【解决方案1】:

    您不能从 BehaviorSubject 中“删除元素”。最接近的方法是在主题中插入一个新数组(这将导致它发出那个新数组。)像这样:

    favoritePhotos.onNext(try favoritePhotos.value().filter { $0.id != currentPhoto.id })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2020-04-24
      相关资源
      最近更新 更多