【发布时间】:2021-04-26 09:11:06
【问题描述】:
我正在实施 Firebase 性能跟踪,因此我创建了一个共享实例并添加了一个字典来保存它们。 我理解的字典不是线程安全的,我需要添加一个调度队列来处理这个。
我应该如何使用 DispatchQueue 来获得最准确的跟踪? 还是我采取了错误的方法来测量痕迹?
import FirebasePerformance
class Tracing {
static let shared = Tracing()
let queue = DispatchQueue(label: "thread-safe-tracing")
var traces = [String: Trace]()
static func startTracing(name: String) {
shared.queue.sync {
if let trace = Performance.startTrace(name: name) {
shared.traces[name] = trace
}
}
}
static func stop(name: String) {
shared.queue.sync {
if let trace = shared.traces[name] {
trace.stop()
shared.traces[name] = nil
}
}
}
}
【问题讨论】:
标签: swift firebase thread-safety firebase-performance dispatch-queue