RxSwift BehaviorRelay auto isDisposed

Posted

技术标签:

【中文标题】RxSwift BehaviorRelay auto isDisposed【英文标题】: 【发布时间】:2019-11-27 02:57:05 【问题描述】:

这是我的代码:

class MapManager: NSObject 

    let removeFenceTask = BehaviorRelay<String?>(value: nil)

    override init() 
        super.init()

        removeFenceTask
            .filter( $0 != nil )
            .debug("removeFenceTask", trimOutput: true)
            .flatMap  (taskId: String?) -> Observable<TaskModel> in
                guard let `taskId` = taskId, let provider = Application.shared.provider else  return Observable.just(TaskModel()) 
                return provider.queryTaskDetail(taskId: taskId).asObservable()
        
        .asObservable()
        .subscribe(onNext:  [weak self] (taskModel) in
            self?.removeFence(taskModel: taskModel)
        ).disposed(by: rx.disposeBag)

    

    func queryTasks(_ currentPage: Int, status: TaskStatus) -> Single<([TaskModel], Bool)> 
        return requestObject(
            .queryTasks(currentPage: currentPage, pageSize: 10, status: status), type: TaskModelWrapper.self)
            .map( (taskModelWrapper) -> ([TaskModel], Bool) in
                let tasks = taskModelWrapper.data?.data ?? []
                tasks.filter( $0.taskStatus == .canceled || $0.taskStatus == .deliveryCompleted || $0.taskStatus == .taskCompleted || $0.taskStatus == .terminated )
                    .forEach  (taskModel) in
                        MapManager.shared.removeFenceTask.accept(taskModel.taskId)
                
                return (tasks, taskModelWrapper.data?.isNoMoreDataShow ?? false)
            )
    

在成功完成 2 次活动后,我终于得到了 isDisposed

控制台:

2019-11-27 10:46:27.203: newFenceTask -> 订阅

2019-11-27 10:46:27.203: removeFenceTask -> 订阅

2019-11-27 10:46:27.204: removeFenceTask -> 事件 next(Optional("123"))

2019-11-27 10:46:27.207: removeFenceTask -> 事件 next(Optional("1908157SRZTT2DGF"))

2019-11-27 10:46:27.295: removeFenceTask -> isDisposed

问题:为什么会发生这种isDisposed

【问题讨论】:

【参考方案1】:

你的rx.disposeBag 没有被处理掉吗?

【讨论】:

以上是关于RxSwift BehaviorRelay auto isDisposed的主要内容,如果未能解决你的问题,请参考以下文章

可从多个 BehaviorRelay RxSwift 观察到

使用 Observable 绑定 BehaviorRelay - RxSwift

在 RxSwift 中修改 behaviorRelay 数组内的属性

RxSwift BehaviorRelay auto isDisposed

RxJava 的 RxSwift 变量或 BehaviorRelay 的内置等效项

RxSwift 与 BehaviorRelay<Any?> 的双向绑定