Reputation: 2148
I implemented Background Fetch feature in my App.
The function called by background fetch includes a delayed function.
However this function doesn't seem to trigger :
let when = DispatchTime.now() + 2
DispatchQueue.main.asyncAfter(deadline: when) {
MyFunction()
}
It seems to trigger however as soon as I wake up the app.
Why this doesn't trigger on background fetch ?
Upvotes: 1
Views: 1868
Reputation: 8737
I don't think you can run in the background on the main queue. Try this (changed main
to global()
):
let when = DispatchTime.now() + 2
DispatchQueue.global().asyncAfter(deadline: when) {
MyFunction()
}
Upvotes: 1
Reputation: 4383
Try this code:
DispatchQueue.global().async {
let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.MyFunction), userInfo: nil, repeats: false)
RunLoop.current.add(timer, forMode: .defaultRunLoopMode)
RunLoop.current.run()
}
Upvotes: 1