2016-09-19 103 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

好吧,它不会停止。但是,如果我在全局变量上设置断点,它会停止。 –