ios - publishSubject 同步异常警告

标签 ios swift rx-swift

我在 Playground 上玩 RxSwift 时遇到了警告。 这是完整的警告消息:

Synchronization anomaly was detected.
- Debugging: To debug this issue you can set a breakpoint in RxSwift/RxSwift/Rx.swift:113 and observe the call stack.
Problem: This behavior is breaking the observable sequence grammar. `next (error | completed)?`
- This behavior breaks the grammar because there is overlapping between sequence events.
Observable sequence is trying to send an event before sending of previous event has finished.
- Interpretation: Two different unsynchronized threads are trying to send some event simultaneously.
This is undefined behavior because the ordering of the effects caused by these events is nondeterministic and depends on the 
operating system thread scheduler. This will result in a random behavior of your program.
- Remedy: If this is the expected behavior this message can be suppressed by adding `.observeOn(MainScheduler.asyncInstance)`
or by synchronizing sequence events in some other way.

这是 Playground 上的代码。

import RxSwift
import Foundation

example("PublishSubject") {
let disposeBag = DisposeBag()
let subject = PublishSubject<String>()


subject.onNext("🐶")
subject.subscribe(onNext: { (value) in
    print(value)
}).disposed(by: disposeBag)
subject.onNext("🐱")

subject.onNext("🅰️")
subject.onNext("🅱️")
DispatchQueue.global(qos: .utility).async {
    for index in 0...10 {
        subject.onNext("1")
    }
          subject.observeOn(MainScheduler.asyncInstance).subscribe(onNext: { (value) in
        print(value)
    }).disposed(by: disposeBag)
}
DispatchQueue.global(qos: .utility).async {
    for index in 0...10 {
        subject.onNext("B")
    }
    subject.observeOn(MainScheduler.asyncInstance).subscribe(onNext: { (value) in
        print(value)
    }).disposed(by: disposeBag)
}
}

我该怎么做才能解决这个警告问题??谢谢

最佳答案

当主题正在处理事件时,您正在发送关于主题的事件。这打破了受试者必须维持的契约。

具体来说,主题中没有任何线程跟踪,因此您必须手动进行。最明显的方法是在您的 onNext 调用周围放置一个递归锁,这样它们在单独的线程上运行时就不会重叠。

let disposeBag = DisposeBag()
let subject = PublishSubject<String>()
let lock = NSRecursiveLock()

subject.onNext("🐶")
subject.subscribe(onNext: { (value) in
    print(value)
}).disposed(by: disposeBag)
subject.onNext("🐱")

subject.onNext("🅰️")
subject.onNext("🅱️")
DispatchQueue.global(qos: .utility).async {
    for index in 0...10 {
        lock.lock()
        subject.onNext("1")
        lock.unlock()
    }
    subject.observeOn(MainScheduler.asyncInstance).subscribe(onNext: { (value) in
        print(value)
    }).disposed(by: disposeBag)
}
DispatchQueue.global(qos: .utility).async {
    for index in 0...10 {
        lock.lock()
        subject.onNext("B")
        lock.unlock()
    }
    subject.observeOn(MainScheduler.asyncInstance).subscribe(onNext: { (value) in
        print(value)
    }).disposed(by: disposeBag)
}

关于ios - publishSubject 同步异常警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54400502/

相关文章:

ios - 每次将 child 添加到 firebase 数据库时都会调用 viewDidLoad

ios - 自定义 UIView 类 - Swift

swift - 响应式设计 : throw vs. 发布错误

ios - 使用 RxSwift 阻塞线程

ios - 出现错误 tableView 的使用不明确(_ :numberOfRowsInSection:)

ios - 无效和重新启动后,Swift 计时器将无法工作

ios - 为什么我的约束在 swift/storyboard 中不能正常工作?

ios - 我无法从 Firebase 数据库中检索数据

ios - Touch Down 不执行它应该执行的操作

ios - Rx swift : How to add gesture to UILabel?