P
P
pistol2018-06-08 16:34:50
Swift
pistol, 2018-06-08 16:34:50

How to add a task to the background in WatchOS 3.4 when the application is inactive?

There is a very simple task - once every 10 seconds to send data from the accelerometer to the backend. Everything works great while the application is active. As soon as the screen goes blank, the app freezes and the data stops sending.
I googled that I have to start a workout session so that the app doesn't freeze. However, starting this session actually gives absolutely nothing. I would be grateful for any help! Fifth day can't get through it ******

//...
override func willActivate() {
    super.willActivate()
    startWorkout()
    startAnglesTracker()
}

func startAnglesTracker() {
    if motion.isAccelerometerAvailable && !motion.isAccelerometerActive {
        motion.accelerometerUpdateInterval = 10
        motion.startAccelerometerUpdates(to: OperationQueue.main) {
            //sending here
        }
    }
}

private func startWorkout() {
    let workoutConfiguration = HKWorkoutConfiguration()
    workoutConfiguration.activityType = .crossTraining
    workoutConfiguration.locationType = .indoor
    do {
        workoutSession = try HKWorkoutSession(configuration: workoutConfiguration)
        workoutSession?.delegate = self
        healthStore.start(workoutSession!)
        vibrate()
    } catch {
        serverData.setText("workoutSession error")
    }
}
//...

extension InterfaceController: HKWorkoutSessionDelegate {
    func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: Error) {

    }

    func workoutSession(_ workoutSession: HKWorkoutSession, didGenerate event: HKWorkoutEvent) {

    }

    func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date) {

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
doublench21, 2018-06-08
@doublench21

This cannot be done and will never be possible. What would some people say you with your application did something with my watch when the application is closed. Well, no) Apple took care of it.
You are given only a certain amount of time in the background, no more.
https://developer.apple.com/library/archive/docume...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question