-2

I've searched on Stack Overflow and of course Google and nothing has come up (which surprises me).

I have an integer variable which constantly changes, and I want to run a certain function when the variable goes past a certain number. Infinite while loops will obviously just slow the phone down and possibly crash.

This variable is the acceleration variable from CMMotionManager, which gives the accelerometer data when displayed/printed. It is NOT a variable set by me.

For example:

if Example > 2 { // Example being the integer variable from another module
    // do stuff here
}

Edit: No, waiting a few seconds and then checking again will NOT work in my situation.

Edit2: It is not a variable set by me, but a variable from another module.

  • How does it change constantly? Is it modified by some other thread/process? Is this supposed to contain some physical measurement which changes? – Tali Sep 11 '15 at 09:49
  • It is the acceleration variable from CMMotionManager that gives you data from the accelerometer, which obviously constantly changes. The variable is not set by me. – Edward Brown Sep 11 '15 at 09:51
  • Don't they have a delegate or notification that will be called on change? If not, use a `NSTimer` to check periodically. The check can be repeated every `0.1 secs` and that should be enough. – Sulthan Sep 11 '15 at 09:54
  • 2
    I am not very familiar with CMMotionManager, but there is a `startAccelerometerUpdatesToQueue(_:withHandler:)` method which looks like a better approach. – And please add the relevant information to the question itself. – Martin R Sep 11 '15 at 09:56
  • No, because the accelerometer data changes literally every 0.1 seconds due to the phone rarely being stable, and with NSTimer, note first edit. – Edward Brown Sep 11 '15 at 09:56
  • Also, I need to detect when the variable gets past a certain number INSTANTLY, it will not work with a time interval. – Edward Brown Sep 11 '15 at 09:59
  • Then you really need some API method to make the Motion manager notify your app on sudden changes in acceleration. Just monitoring some variable yourself is not going to work. – Tali Sep 11 '15 at 10:51
  • If you want to detect sudden drops or crashes of the device, then this really has to be supported by the hardware and OS. You require the HW to detect that on its own, generating an interrupt which will be routed to your App by the OS. From looking at CMMotionManager, I don't see any such support in iOS yet. – Tali Sep 11 '15 at 10:58
  • What event do you really want to detect? – Tali Sep 11 '15 at 11:25

1 Answers1

2

Have you checked willSet and didSet options?

var _test : Int = 0 {

    //First this
    willSet {
        println("Old value is \(_test), new value is \(newValue)")
    }

    //value is set

    //Finaly this
    didSet {
        println("Old value is \(oldValue), new value is \(_test)")
    }
}

More details here.

Community
  • 1
  • 1
Josip B.
  • 2,434
  • 1
  • 25
  • 30
  • I don't exactly understand how this actually checks if a variable is above or below a certain number, could you elaborate? – Edward Brown Sep 11 '15 at 09:49
  • 1
    @EdwardBrown: Just write your `if condition` inside the `didSet` block. – Luca Angeletti Sep 11 '15 at 09:52
  • You can compare newValue to see. I.e if newValue > 2. P.s. in example above you compared Int with 0.2 which is Float value and comparison Int with Float isn't the happiest solution you would want. – Josip B. Sep 11 '15 at 09:53