title: Why does Xcode 8.1 + Metal. crash the Timer I use to manipulate my 3D model?
body: <p>I have an arcball manipulator that uses a <code>Timer</code> to create a smoothly decelerating attenuation of the current rotation angle. It has been working just fine in my <code>GL</code> app. In <code>Metal</code>, it crashes the app.</p>
  
  <p>Here is how I instantiate the timer:</p>
  
  <pre><code>rotationTimer = Timer.scheduledTimer(
  timeInterval: TimeInterval(kRotationRate), 
  target: self,
  selector: Selector(("rotationTimerHandler")),
  userInfo: [ "radiansBegin":radians, "radians":radians, "radiansEnd":0, "counter":0 ],
  repeats: true)
  </code></pre>
  
  <p>The function <code>rotationTimerHandler</code> decrements the angle - <code>radians</code> - and then builds a transformation matrix from it that gets include in the update function of my render draw loop.</p>
  
  <p>I suspect I need to do some synchronization with timing of the <code>Metal</code> draw loop but I see no way to do that. How do I get <code>Timer</code> and <code>Metal</code> to play nice together?</p>
  
  <h2>UPDATE 0</h2>
  
  <p>Per Warren's question storm :-) here is a bit more context. </p>
  
  <p>My app's view is an <code>MTKView</code> with 2 properties. </p>
  
  <p>A renderer that conforms to <code>MTKViewDelegate</code> protocol and implements:</p>
  
  <pre><code>func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize)
  func draw(in view: MTKView)
  </code></pre>
  
  <p>Drawing is driven by the MTKView's internal display link timer. Everything runs on the main thread. No async calls.</p>
  
  <p>and an arcball. An object that maps screen gestures to a quaternion to a 3D rotation matrix. The arcball is a pan gesture handler for the view:</p>
  
  <pre><code>addGestureRecognizer(UIPanGestureRecognizer.init(
      target: arcBall,
      action: #selector(EIArcball.arcBallPanHandler)))
  </code></pre>
  
  <p>}</p>
  
  <p>When the arcball's pan gesture handler enter's an end state I instantiate a timer for N seconds to continue generationg quaternions/matrices based on the angle/axis of rotation when panning ended for a pleasing, gliding effect.</p>
  
  <p>Ok, the renderer (which conforms to <code>MTKViewDelegate</code>). The draw loop:</p>
  
  <pre><code>public func draw(in view: MTKView) {
  
      update(view: view as! EIView, drawableSize: view.bounds.size)
  
      // final pass
      if let passDescriptor = view.currentRenderPassDescriptor, let drawable = view.currentDrawable {
  
          passDescriptor.colorAttachments[ 0 ].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0)
  
          let commandBuffer = commandQueue.makeCommandBuffer()
  
          let renderCommandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: passDescriptor)
  
          renderCommandEncoder.setFrontFacing(.counterClockwise)
          renderCommandEncoder.setTriangleFillMode(.fill)
          renderCommandEncoder.setCullMode(.none)
  
          renderCommandEncoder.setRenderPipelineState(heroModelPipelineState)
          renderCommandEncoder.setVertexBuffer(heroModel.vertexMetalBuffer, offset: 0, at: 0)
          renderCommandEncoder.setVertexBuffer(heroModel.metallicTransform.metalBuffer, offset: 0, at: 1)
          renderCommandEncoder.setFragmentTexture(heroModelTexture, at: 0)
          renderCommandEncoder.drawIndexedPrimitives(
                  type: .triangle,
                  indexCount: heroModel.vertexIndexMetalBuffer.length / MemoryLayout&lt;UInt16&gt;.size,
                  indexType: MTLIndexType.uint16,
                  indexBuffer: heroModel.vertexIndexMetalBuffer,
                  indexBufferOffset: 0)
  
          renderCommandEncoder.endEncoding()
  
          commandBuffer.present(drawable)
          commandBuffer.commit()
      }
  
  }
  </code></pre>
  
  <p>Nothing particularly interesting here. Notice the call:</p>
  
  <pre><code>update(view: view as! EIView, drawableSize: view.bounds.size)
  </code></pre>
  
  <p>Here is <code>update</code></p>
  
  <pre><code>func update(view: EIView, drawableSize:CGSize) {
  
      heroModel.metallicTransform.update(camera: camera, transformer: {
          return view.arcBall.rotationMatrix * GLKMatrix4MakeScale(150, 150, 1)
      })
  
  }
  </code></pre>
  
  <p>function <code>heroModel.metallicTransform.update(...)</code> builds a model-view-projection matrix used in my vertex shader. This is a toy app that draws a quad scaled in x-y by 150 that is manipulated (rotated) by pan gestures fed to the arcball.</p>
  
  <p>In <code>update</code> I make no attempt to schedule the feeding of arcball's transformation matrix to update. That seems like a code smell.</p>
  
  <p>Here is the crasher screendump:
  <a href="https://i.stack.imgur.com/wFxrH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wFxrH.png" alt="enter image description here"></a></p>
  
  <p>The more I think about this it seems I should just make my draw calls explicitly since I have no real need to be constantly redraw-ing.
  1) When panning just call renderer.draw().
  2) After panning ends call render.draw() from my Timer handler.</p>
  
  <h2>UPDATE 1</h2>
  
  <p>Here is more stack trace. Just a whole lot of assembler:</p>
  
  <p><a href="https://i.stack.imgur.com/LOwlR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LOwlR.png" alt="enter image description here"></a></p>
  
  <h2>UPDATE 2</h2>
  
  <p>The output of bt (backtrace):</p>
  
  <p><a href="https://i.stack.imgur.com/KfgoB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KfgoB.png" alt="enter image description here"></a></p>
  
  <p>I think I will experiment running <code>Metal</code> <strong>without</strong> the internal timer and call <code>MTKView.draw()</code> explicitly. No need for a timer-based approach in such a simple app and issues with my <code>NSTimer</code> based Arcball.</p>