문제

I make use of CMMotionManager in order to access the gyroscope data for iOS. I see that there are 2 methods :

startGyroUpdates 
startGyroUpdatesToQueue:withHandler:

to start receiving the gyro updates. How can we differentiate between calling these two methods. What are the situations when the either of them can be called? IS there any significance of one over the other?

Any help appreciated,

도움이 되었습니까?

해결책

A queue is used to guarantee that all events are processed, even when the update interval you set in deviceMotionUpdateInterval is producing events at a faster rate than you can process in real time. If you don't mind missing events, it doesn't matter which one of the two you use, just discard them.

The relevant Apple doc is the Core Motion section of the Event Handling Guide:

For each of the data-motion types described above, the CMMotionManager class offers two approaches for obtaining motion data, a push approach and a pull approach:

  • Push. An application requests an update interval and implements a block (of a specific type) for handling the motion data; it then starts updates for that type of motion data, passing into Core Motion an operation queue as well as the block. Core Motion delivers each update to the block, which executes as a task in the operation queue.

  • Pull. An application starts updates of a type of motion data and periodically samples the most recent measurement of motion data.

The pull approach is the recommended approach for most applications, especially games; it is generally more efficient and requires less code. The push approach is appropriate for data-collection applications and similar applications that cannot miss a sample measurement.

It's not on your question, but I wonder if you want the raw x,y,z rotation or the more useful pitch,roll,yaw. For the later use startDeviceMotionUpdatesToQueue:withHandler: instead startGyroUpdatesToQueue:withHandler:.

다른 팁

Edit: See Tommy's comment on this answer. My assumption of the delegate pattern was wrong.

I'm not particularly familiar with CMMotionManager, but from the naming here's my guess:

  • startGyroUpdates
    Delivers gyroscope updates by invoking delegate methods on the main thread.
  • startGyroUpdatesToQueue:withHandler:
    Delivers gyroscope updates by invoking the handler block on the given queue.

The first would be the pre-block style using delegates, and the second would be the blockified version based on GCD.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top