2016-09-19 48 views
0

在我的Cocoa應用程序中,一個屬性在它不應該被改變時會被修改。爲了找出它發生的地方,我提出了有助於縮小問題的斷言(我嘗試了「觀察變量」,但無法理解它的使用方式)。但是現在我面臨着一個似乎與它應該相反的主張(請參閱截圖)。斷言失敗應該通過:爲什麼?

我正在使用自定義運算符(請參見下文)讓代碼在另一個線程中運行。

我錯過了什麼,我能做些什麼來推進?

State of Xcode when the program crashes. 當程序崩潰時Xcode的狀態。

Stack trace. 堆棧跟蹤。

import Foundation 
infix operator ~> 

/** 
Executes the lefthand closure on a background thread and, 
upon completion, the righthand closure on the main thread. 
Passes the background closure's output, if any, to the main closure. 
*/ 
func ~> <R> (
    backgroundClosure: @escaping() -> R, 
    mainClosure:  @escaping (_ result: R) ->()) 
{ 
    queue.async { 
     let result = backgroundClosure() 
     DispatchQueue.main.async { 
      mainClosure(result) 
     } 
    } 
} 

private let queue = DispatchQueue(label: "Edk4ServerRequests") 

運營商定製的定義。 (代碼改編自https://ijoshsmith.com/2014/07/05/custom-threading-operator-in-swift/

+0

執行將停止我增加了有關樣品中使用的自定義「〜>」運營商的重要信息。 –

+0

我重寫了我的代碼以確保'isUpdating'不會被另一個線程修改,現在它可以工作。不過,我想了解在提供的截圖中,斷言失敗時,調試器顯示「isUpdating」的值爲「false」。 –

回答

1

您可以在屬性聲明中添加斷點。當屬性值將被改變

enter image description here

+0

這將有所幫助。我只是嘗試在「private var isUpdating:Bool」(不確定這是你的意思),再次運行一個斷點,但沒有任何改變。 –

+0

就是這樣。當你的'isUpdating'將被改變時,執行將會停止。 – CZ54

+0

好吧,它不會停止。但是,如果我在全局變量上設置斷點,它會停止。 –