scheduled timer with CoreMotion

38 views Asked by At

I'm using the CoreMotion framework, and it works, but now I'd like to add a timer that loads a method after 5 seconds.

If 5 seconds have not passed load another method.

the problem is that in the debug area it doesn't print "STOPMAP" or "STARTMAP" so I think the timer doesn't work, where am I wrong?

import UIKit

import CoreMotion

private let motionActivityManager = CMMotionActivityManager()

class ViewController: UIViewController {

    @IBOutlet weak var lblActivity: UILabel!
    
    var second = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
           let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(calculateSeconds), userInfo: nil, repeats: true)
        
        motionActivityManager.startActivityUpdates(to: OperationQueue.main) { (activity: CMMotionActivity?) in
            guard let activity = activity else { return }
            DispatchQueue.main.async {
                if activity.stationary {
                    self.lblActivity.text = "Stationary"
                    print("Stationary")
                    
                    calculate()

                } else if activity.walking {
                    self.lblActivity.text = "Walking"
                    print("Walking")
                } else if activity.running {
                    self.lblActivity.text = "Running"
                    print("Running")
                } else if activity.automotive {
                    self.lblActivity.text = "Automotive"
                    print("Automotive")
                }
            }
        }
        
        
    }
    
    @objc func calculateSeconds() {
         second += 1
    }
    
    func calculate() {
         if second > 5 {
             stopMap()
         } else {
             startMap()
         }

         second = 0
         timer.invalidate()
         timer = nil
    }
    
    func stopMap() {
        print("STOPMAP")
    }
    
    func startMap() {
        print("STARTMAP")
    }

}
1

There are 1 answers

1
arslan raza On

Here is the solution, this will resolve the issue of timer. Test it on real device.

import CoreMotion

import UIKit

private let motionActivityManager = CMMotionActivityManager()

class ViewController: UIViewController {

    var second = 0
    var timer: Timer?

    public func addTimer() {
        motionActivityManager.startActivityUpdates(to: OperationQueue.main) { (activity: CMMotionActivity?) in
            guard let activity = activity else { return }
            DispatchQueue.main.async {
                print(activity)
                if activity.stationary {
                    print("Stationary")

                    // Add a 5-second timer when the activity becomes "Stationary"
                    self.startTimer()
                } else if activity.walking {
                    print("Walking")
                    // Stop the timer when the activity changes
                    self.stopTimer()
                } else if activity.running {
                    print("Running")
                    // Stop the timer when the activity changes
                    self.stopTimer()
                } else if activity.automotive {
                    print("Automotive")
                    // Stop the timer when the activity changes
                    self.stopTimer()
                }
            }
        }
    }

    @objc func updateTimer() {
        second += 1
        if second >= 5 {
            // Execute your action when the timer reaches 5 seconds
            self.timerFired()
        }
    }

    func startTimer() {
        // Create a 1-second repeating timer
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
    }

    func stopTimer() {
        // Stop and invalidate the timer
        timer?.invalidate()
        timer = nil
        second = 0
    }

    func timerFired() {
        // Implement the action you want to take when the timer fires (after 5 seconds)
        print("Timer fired after 5 seconds!")
        // You can add your logic here, such as stopping the activity updates.
        // For example, self.motionActivityManager.stopActivityUpdates()
    }
}