How to start and stop Firebase performance traces from a singleton dictionary with DispatchQueue

406 Views Asked by At

I am in the process of implementing Firebase performance traces, so i made a shared instance and added a dictionary to hold them. What i understand dictionaries are not thread safe and i need to add a dispatch queue to handle this.

How should i be using DispatchQueue to get the most accurate trace? Or am I taking the wrong approach to measure traces?

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
            }
        }
    }

  }
1

There are 1 best solutions below

0
On

you are tried with synchronus method please try with Asynchronus and let me know.