2017-04-18 27 views
1

當在Debug方案中運行時,如果你的代碼是這樣的,則第30行有一個致命錯誤。如何抑制RxSwift中的指定錯誤?

https://github.com/ReactiveX/RxSwift/blob/master/RxSwift/Observables/Implementations/Sink.swift

rxFatalError("Warning: Recursive call or synchronization error!") 

如果我選擇從調試運行方案發布。致命錯誤不會顯示。但我想知道我是否可以採取一些行動來壓制它。

class ViewController4: UIViewController { 
    var v = Variable(0) 
    var disposeBag = DisposeBag() 
    var notiBag = DisposeBag() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     v.asObservable() 
      .subscribe(onNext: { _ in 
       let noti = Notification(name: MyNotificationName) 
       NotificationCenter.default.post(noti) 
      }) 
      .disposed(by: disposeBag) 

     NotificationCenter.default.rx.notification(MyNotificationName) 
      .subscribe(onNext: { [unowned self] _ in 
       if self.v.value == 10 { self.notiBag = DisposeBag() } 
       else { self.v.value += 1 } // this line cause the issue 
       print(self.v.value) 
       self.counterTextView.text! += "\(self.v.value)\n" 
      }) 
      .disposed(by: notiBag) 
     v.value = 0 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBOutlet weak var counterTextView: UITextView! 
} 

let MyNotificationName = Notification.Name.init(rawValue: "My Notification Name") 
+0

它看起來像你的代碼監聽通知'MyNotificationName',然後重新發布它的10倍?那真的是你想要的嗎? –

回答

1

,因爲它要求rxFataError只有當你編譯調試只出現在調試模式下這致命的錯誤。它的定義是這樣的:

#if DEBUG 
    if AtomicIncrement(&_numberOfConcurrentCalls) > 1 { 
     rxFatalError("Warning: Recursive call or synchronization error!") 
    } 

    defer { 
     _ = AtomicDecrement(&_numberOfConcurrentCalls) 
    } 
#endif 

我剛剛在RxSwift更新(3.4.0)後出現這個致命錯誤。我的代碼更新了併發隊列中的變量值。更改爲串行隊列修復了此次崩潰。

蒂埃裏

1

您可以不使用Variable相同的行爲。

let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated) 
    NotificationCenter.default.rx.notification(MyNotificationName) 
     .take(9) 
     .observeOn(scheduler) 
     .subscribe(onNext: { _ in 
      let noti = Notification(name: MyNotificationName) 
      print("foo") 
      NotificationCenter.default.post(noti) 
     }).disposed(by: bag) 

    let noti = Notification(name: MyNotificationName) 
    NotificationCenter.default.post(noti) 
+0

你可以仔細檢查你的答案嗎?我試過了,但沒有奏效。 –

+0

在此背景下定義「工作」。你想要做什麼? –

+0

您的代碼仍然觸發致命錯誤。 –

1

我將@thierryb的答案標記爲正確答案。已知的正確答案如下。

NotificationCenter.default.rx.notification(MyNotificationName) 
    .observeOn(MainScheduler.asyncInstance) 
    .subscribe(onNext: { [unowned self] _ in 
     if self.v.value == 10 { self.notiBag = DisposeBag() } 
     else { self.v.value += 1 } // this line cause the issue 
     print(self.v.value) 
     self.counterTextView.text! += "\(self.v.value)\n" 
    }) 
    .disposed(by: notiBag) 

NotificationCenter.default.rx.notification(MyNotificationName) 
    .subscribe(onNext: { [unowned self] _ in 
     DispatchQueue.main.async { [unowned self] in 
      if self.v.value == 10 { self.notiBag = DisposeBag() } 
      else { self.v.value += 1 } // this line cause the issue 
      print(self.v.value) 
      self.counterTextView.text! += "\(self.v.value)\n" 
     } 
    }) 
    .disposed(by: notiBag)