How does CMMotionActivity startDate swift work?

161 Views Asked by At

So I'm developing in swift and trying to find out the duration of a device being stationary. I ran into startDate function and trying to print it but it gives the time and date which is 5 hours later. For example the local time is now 2019-12-16 21:38:05 and it prints 2019-12-17 02:38:07. Not sure if there is something wrong with the way I'm using the function or it just doesn't have the functionality that I'm expecting. Here is the code:

private let activityManager = CMMotionActivityManager()

func setUpActivityNotification(){
        var timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(updateActivity), userInfo: nil, repeats: true)

    }
@objc func updateActivity(){
        activityManager.startActivityUpdates(to: OperationQueue.main) {
            (activity: CMMotionActivity?) in

            guard let activity = activity else { return }
            print(activity.startDate)
            DispatchQueue.main.async {
                if activity.walking {
                    print("Walking")

                } else if activity.stationary {
                    print("Stationary")
                }
            }
        }
    }
0

There are 0 best solutions below