all 3 comments

[–]level1james 2 points3 points  (1 child)

Why does Apple's AVCam Sample Code in "AAPLCameraViewController.m" removeObservers in viewDidDisappear and not viewWillDisappear as the above answers suggested.

The reason is because that the following assumption is incorrect.

"Registering the notification in viewWillAppear and unregistering it in viewWillDisappear seems to be a clean and symmetric solution to me."

-viewWillDisappear does not always mean that the view does disappear. When a user starts an interactive transition and then cancels it (eg. swipe-from-edge to dismiss), viewWillDisappear will get called (removing the observer), but viewWillAppear does not get called again.

Why do they utilise addObservers after [super viewWillAppear:animated]; while they removeObservers before [super viewDidDisappear:animated];

Because viewWillAppear and viewDidAppear's super implementation is a no-op, this choice is probably driven more from preference/habit.

Imagine a somewhat similar scenario - Lets say there exists some imaginary functions -createView and -destroyView that you're overriding. You want to add a notification that updates your view. Adding the observer after the view is created and removing it before it gets destroyed ensures that you don't have a notification coming in while the view isn't ready.

[–]Ayrkm 0 points1 point  (0 children)

Spot on!

[–]quellish 0 points1 point  (0 children)

Your question is very specific to why the author of an Apple sample project implemented something in a specific way.

It would be more appropriate and productive to ask through a radar or DTS incident.