假设我有这种功能和 DispatchQueue 逻辑。假设当synchLatest()
被调用时它会触发"Code Block 1"
两次。
应该怎么"Code Block 1"
做?1.7 seconds
5 seconds
let synchQueue = DispatchQueue(label: "com.dmapp.synchQueue", qos: .default, attributes: .concurrent)
let synchProcessQueue = DispatchQueue(label: "com.dmapp.processQueue", qos: .default, attributes: .concurrent)
func synchLatest() {
while(someconditions) {
synchQueue.async {
...
let response = try grpcCall.receive()
...
synchProcessQueue.async {
....
measure("Code Block 1", {
if response.data.nickname != "" {
// Store in UserDefaults
}
})
....
}
}
}
}
@discardableResult
static func measure<A>(name: String = "", _ block: () -> A) -> A {
let startTime = CACurrentMediaTime()
let result = block()
let timeElapsed = CACurrentMediaTime() - startTime
print("Time: \(name) - \(timeElapsed)")
return result
}
我是否以错误的方式在这里测量代码执行时间?