Ios publishSubject同步异常警告

Ios publishSubject同步异常警告,ios,swift,rx-swift,Ios,Swift,Rx Swift,我和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

我和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.
这是操场上的代码

导入RxSwift
进口基金会
示例(“PublishSubject”){
设disposeBag=disposeBag()
let subject=PublishSubject()

No.No.[()当你在处理一个事件的过程中,你在你的主题上发送一个事件。这违反了主体必须维护的契约。

特别是,主题中没有任何类型的线程跟踪,因此您必须手动执行。最明显的方法是在
onNext
调用周围放置递归锁,以便在单独的线程上运行时不会重叠

let dispebag=dispebag()
let subject=PublishSubject()
let lock=NSRecursiveLock()

subject.onNext(“
导入PlaygroundSupport
并在需要时添加这一行
PlaygroundPage.current.NeedsDefiniteExecution=true
这不是关于游乐场,而是关于Rxswift,我的项目中也有这个问题…奇妙的thx我不知道NSRecursiveLock。如果可以避免使用
主题,当然可以节省我的时间ect
首先,那会更好。很高兴我能帮上忙。你是说PublishSubject?为什么我不应该?避免主题会让你的代码更干净,并有助于确保此类问题不会发生。也就是说,在这种特殊情况下,你可能无法避免使用主题,但如果可以的话,你应该这样做。有一个更复杂的问题ex-answer(),但在开始获取答案之前,我必须多次阅读该答案,我肯定不能总结出比上面更好的答案。@jcpennypincher这是一种在所有其他方法都失败时使用的技巧,但请确保您知道使用该方法时会发生什么。
.observeOn(MainScheduler.asyncInstance)
将事件路由到后台串行线程,然后再返回到主线程。这比从第一原理解决问题要昂贵得多,因此请谨慎使用。