Swift 3 - How to make timer work in background

后端 未结 9 445
忘掉有多难
忘掉有多难 2020-11-30 09:43

i am trying to do an application which can make a timer run in background.

here\'s my code:

let taskManager = Timer.scheduledTimer(timeInterval: 10,          


        
9条回答
  •  爱一瞬间的悲伤
    2020-11-30 10:22

    As others pointed out, Timer cannot make a method run in Background. What you can do instead is use while loop inside async task

    DispatchQueue.global(qos: .background).async {
                    while (shouldCallMethod) {
                        self.callMethod()
                        sleep(1)
                    }
    }
    

提交回复
热议问题