title: Adapting Shake Code from 'Beginning Iphone 4 Development' Causing Crash
body: <p>The code below is a near replica of the Shake and Bake example from Beginning Iphone 4 Development.  It is lacking a BOOL if statement before CMAcceleration acceleration = accelerometerData.acceleration; because I want the person to shake and refresh the results as many times as they want. I  have a button that executes the same code flawlessly. When I run the code and shake the iphone, it crashes. What am I missing that will make this work?  Can you not run the same code with a button as with the shake method?</p>
  
  <p>Example.h</p>
  
  <pre><code>#import &lt;CoreMotion/CoreMotion.h&gt;
  #define kAccelerationThreshold 1.7
  #define kUpdateInterval (1.0f/10.0f)
  
  @interface exampleViewController : UIViewController  {
  CMMotionManager *motionManager;
  }
  @property (retain,nonatomic) CMMotionManager *motionManager;
  @end
  </code></pre>
  
  <p>Example.m</p>
  
  <pre><code>@synthesize motionManager;
  
  -(void)viewDidLoad {
  
  self.motionManager = [[[CMMotionManager alloc] init] autorelease];
  motionManager.accelerometerUpdateInterval = kUpdateInterval;
  NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
  [motionManager startAccelerometerUpdatesToQueue:queue
                                      withHandler:
   ^(CMAccelerometerData *accelerometerData, NSError *error){
       if (error) {
           [motionManager stopAccelerometerUpdates];
       } else {
           CMAcceleration acceleration = accelerometerData.acceleration;
           if (acceleration.x &gt; kAccelerationThreshold 
              || acceleration.y &gt; kAccelerationThreshold 
              || acceleration.z &gt; kAccelerationThreshold){
  
  // There is a bunch of other stuff here, but it all works using a button called shake....
              example4.hidden = NO;
              select1.text = first;
              display.text = [[NSString alloc] initWithFormat: @"%@", first];
  
           }
       }
   }];
  }
  
  - (void)dealloc
  {
  [super dealloc];
  [motionManager release];
  }
  
  - (void)viewDidUnload {
  [super viewDidUnload];
  self.motionManager = nil;
  }
  
  @end
  </code></pre>