title: Swift Accelerometer keeps crashing with ScrollView
body: <p><strong>EDIT:</strong> I have simplified the code and added calls to NSThread.isMainThread(), to see if this was the problem. See more extensive edit below</p>
  
  <p>I'm working on a fairly simple app to assist a professor in research over the summer. The app intends to determine word difficulty in sentences based on the accelerometer in the iPad. </p>
  
  <p>Essentially, the user will tilt the iPad, thus creating a non-zero acceleration, and the text, which is situated in a <code>UILabel</code> placed within a <code>scrollView</code> will scroll accordingly.</p>
  
  <p>This works excellently 99% of the time. In almost all of our tests, it works perfectly, it goes through the entire text without issue, and nothing bad happens. Very rarely however, it just breaks, throwing an error of <strong>EXC_BAD_ACCESS</strong>. I want to stress that on the rare occasions it does break, there is no apparent pattern, it sometimes happens in the middle of scrolling, near the end, or at the start.</p>
  
  <p>Obviously I would like the app to be bug free, and this is a fairly major one which I just can't figure out, so any help you can give would be greatly appreciated.</p>
  
  <p>Here is the total code for my <code>ScrollingLabel Class</code> (the bug always happens at the end of the <code>startScrolling class</code>).</p>
  
  <pre><code>import Foundation
  import UIKit
  import QuartzCore
  import CoreMotion
  
  
  public class ScrollingLabel {
  
  //Instantiation of scroll view and label
  var baseTextLabel:UILabel!
  var baseScrollView:UIScrollView!
  var frame:CGRect!
  
  
  //Instantiation of accelerometer materials
  var motionManager=CMMotionManager()
  var queue=NSOperationQueue()
  //To rectify the issue, I have changed this to:
  //var queue=NSOperationQueue.mainQueue(), SEE EDIT BELOW
  
  init(frame:CGRect) {
      /*Initializes the object by calling 3 private setup functions,
      each dealing one with a specific feature of the final label, and
      finally calling the scroll function to activate the accelerometer
      control*/
      setupFrame(frame)
      setupLabel()
      setupScroll()
      startScrolling()
  }
  
  
  
  private func startScrolling() {
      //The main accelerometer control of the label
      println(NSThread.isMainQueue) //THIS RETURNS TRUE
  
      //Allows the start orientation to become default
      var firstOrientation:Bool
      var timeElapsed:Double=0
      if letUserCreateDefaultOrientation {firstOrientation=true}
      else {firstOrientation=false}
      var standardAccel:Double=0
  
      //Begins taking updates from the accelerometer
      if motionManager.accelerometerAvailable{
          motionManager.accelerometerUpdateInterval=updateTimeInterval
          motionManager.startAccelerometerUpdatesToQueue(self.queue, withHandler: { (accelerometerData, error:NSError!) -&gt; Void in
              println(NSThread.isMainQueue) //THIS RETURNS FALSE
              //Changes the input of acceleration depending on constant control variables
  
              var accel:Double
              if self.timerStarted {
                  timeElapsed+=Double(self.updateTimeInterval)
              }
              if !self.upDownTilt {
                  if self.invertTextMotion {accel = -accelerometerData.acceleration.y}
                  else {accel = accelerometerData.acceleration.y}
              }
              else {
                  if self.invertTextMotion {accel = -accelerometerData.acceleration.x}
                  else {accel = accelerometerData.acceleration.x}
              }
  
              //Changes default acceleration if allowed
              if firstOrientation {
                  standardAccel=accel
                  firstOrientation=false
              }
              accel=accel-standardAccel
  
              //Sets the bounds of the label to prevent nil unwrapping
              var minXOffset:CGFloat=0
              var maxXOffset=self.baseScrollView.contentSize.width-self.baseScrollView.frame.size.width
  
              //If accel is greater than minimum, and label is not paused begin updates
              if !self.pauseScrolling &amp;&amp; fabs(accel)&gt;=self.minTiltRequired {
  
                  //If the timer has not started, and accel is positive, begin the timer
                  if !self.timerStarted&amp;&amp;accel&lt;0{
                      self.stopwatch.start()
                      self.timerStarted=true
                  }
  
                  //Stores the data, and moves the scrollview depending on acceleration and constant speed
                  if self.collectData {self.storeIndexAccelValues(accel,timeElapsed: timeElapsed)}
                  var targetX:CGFloat=self.baseScrollView.contentOffset.x-(CGFloat(accel) * self.speed)
                  if targetX&gt;maxXOffset {targetX=maxXOffset;self.stopwatch.stop();self.doneWithText=true}
                  else if targetX&lt;minXOffset {targetX=minXOffset}
                  self.baseScrollView.setContentOffset(CGPointMake(targetX,0),animated:true)
                  if self.baseScrollView.contentOffset.x&gt;minXOffset&amp;&amp;self.baseScrollView.contentOffset.x&lt;maxXOffset {
                      if self.PRIVATEDEBUG {
                          println(self.baseScrollView.contentOffset)
                      }
                  }
              }
          })
      }
  }
  </code></pre>
  
  <p>When it does crash, it happens at the end of the <code>startScrolling</code>, when I set the content Offset to target X. If you need more information I am happy to provide it, but as the bug happens so rarely I don't have anything to say about specifically when it occurs or anything like that... it just seems random.</p>
  
  <p><strong>EDIT:</strong> I have simplified the code to just the pertinent parts, and added the two locations where I called NSThread.isMainQueue(). When called on the first line of <code>startScrolling</code>, .isMainQueue() returns <strong>TRUE</strong>, but then when called inside <code>motionManager.startAccelerometerUpdatesToQueue</code> it returns <strong>FALSE</strong>. </p>
  
  <p>To rectify this, I have changed self.queue from just a NSOperationQueue() to NSOperationQueue.mainQueue(), and after making this switch, the second .isMainThread() call (the one inside <code>motionManager.startAccelerometerUpdatesToQueue</code>) now returns <strong>TRUE</strong> as we hoped for.</p>