Home > Net >  Update Live Activity once per second - Swift
Update Live Activity once per second - Swift

Time:12-01

I'm searching for a way to update the Live Activity every second or every 2 seconds without using Push Notifications while keeping the app in background.

Do you have any suggestions? I tried something like this, but after few seconds it's stop working:

       var bgTask = UIBackgroundTaskIdentifier(rawValue: 1324)
        bgTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
            UIApplication.shared.endBackgroundTask(bgTask)
        })
        let timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(updateInfo), userInfo: nil, repeats: true)
        RunLoop.current.add(timer, forMode: .default)

CodePudding user response:

As mentioned in the documentation, "The delay between the time you schedule a background task and when the system launches your app to run the task can be many hours".

Unfortunately, background tasks are not suitable for your needs. Using Push notifications is the only way (that I know of at least) to achieve what you are trying to do.

CodePudding user response:

Top Widget ,Mico (Mico have some Bugs)

  • Related