Search code examples
swifttimeraudiokitauv3

Timers conflicst in AUv3 in Audiokit when running some instances of the AUv3


I am working in an AUv3 in Audiokit. The Auv3 has a timer that perform actions. The timer is activated on viewDidLoad. I have try with Timer, usleep, DispatchQueue.asyncAfter and DispatchSourceTimer. All them works fine. The problem is when running some instances of the AUv3 the timer duplicate the fire time by the number of AUv3 instances or don't work correctly. There are no problem with cpu charge. Is there any way to run a timer in some instances of Auv3 at the same time without conflicts between them?. Some of my timers:

func queue1() {
    
    let currentDateTime = Date()
    DispatchQueue.init(label: String(currentDateTime.timeIntervalSinceReferenceDate)).asyncAfter(deadline: .now() + .milliseconds(Int(1000 / self.slidervalue))) {
     
        if self.closeQueue == 0 {
             self.queue1()
        self.makeTimer()
       }
      }
}
func usleep1(){
    Darwin.usleep(10000)
    usleep1()
    makeTimer()
    
}
func timer1(){
    timer = Timer.scheduledTimer(timeInterval: Double(timeInterval), target: self, selector: #selector(self.makeTimer), userInfo: nil, repeats: true)
     }

I have try with main, global and user queues. I don't know why an AUv3 timer affects to the others instances. Any idea?. Finally I have use AKSequencer and AKMetronme but the problem is the same. Thanks.


Solution

  • The problem likely isn't the Timer -- it's the fact that you probably (since it's not shown here) are relying on some sort of singleton in your code.

    AUv3 instances need to have truly unique instances of their dependencies to function independently.