Behavior of beginGeneratingDeviceOrientationNotifications


#1

I have questions about the behavior of the beginGeneratingDeviceOrientationNotifications property.

I implemented HeavyRotation just fine (iPad implementation) and then experimented a bit to reinforce the concepts. I commented out the line

but kept the addObserver:selector:name:object message.

To my surprise, the notifications were still received with the correct orientations. I expected the device orientation as passed to the notification receiver (orientation of type UIDeviceOrientation) to have a value of 0 (unknown orientation). Instead, it correctly reflected the device orientation (both on the simulator as well as my iPad). I looked at the documentation and noticed it said…

[quote]You must call this method before attempting to get orientation data from the receiver. This method enables the device’s accelerometer hardware and begins the delivery of acceleration events to the receiver. The receiver subsequently uses these events to post UIDeviceOrientationDidChangeNotification notifications when the device orientation changes and to update the orientation property.

You may nest calls to this method safely, but you should always match each call with a corresponding call to the endGeneratingDeviceOrientationNotifications method.[/quote]

This piqued my interest. First, I assume this is a global property of the device (the singleton [UIDevice currentDevice]; The documentation for the property says…

[quote]@property (nonatomic, readonly, getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications
Discussion
If the value of this property is YES, the shared UIDevice object posts a UIDeviceOrientationDidChangeNotification notification when the device changes orientation. If the value is NO, it generates no orientation notifications. Device orientation notifications can only be generated between calls to the beginGeneratingDeviceOrientationNotifications and endGeneratingDeviceOrientationNotifications methods.[/quote]

How is it generating the correct notifications without setting the beginGeneratingDeviceOrientationNotification property?

What is the scope of this property? I started thinking that another application set the property and didn’t call endGeneratingDeviceOrientationNotifications. To cover that case, I called endGeneratingDeviceOrientationNotifications without calling beginGeneratingDeviceOrientationNotification. HeavyRotation still generated device orientation notifications.

How does nesting the calls work (as discussed in the documentation)?

[quote][device beginGeneratingDeviceOrientationNotifications]

[device beginGeneratingDeviceOrientationNotifications]

[device endGeneratingDeviceOrientationNotifications]

[device endGeneratingDeviceOrientationNotifications][/quote]

Finally, the author did not mention the need for sending the message endGeneratingDeviceOrientationNotifications. Does each application have its own instance of UIDevice currentDevice? That way, it goes out of scope when the application terminates?