As seen in this stackoverflow answer about
"NSNotificationCenter with respect to ViewWillAppear and ViewWillDisappear"
We are pointed in the direction of the below as the preferred approach
"Registering the notification in viewWillAppear and unregistering it in viewWillDisappear seems to be a clean and symmetric solution to me."
This is approach is also suggested in this other stackoverflow answer.
My Question is twofold
Why does Apple's AVCam Sample Code in "AAPLCameraViewController.m"
removeObservers
in viewDidDisappear and notviewWillDisappear
as the above answers suggested.Why do they utilise
addObservers
after[super viewWillAppear:animated];
while theyremoveObservers
before[super viewDidDisappear:animated];
Code Extracted from "AAPLCameraViewController.m"
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
dispatch_async(self.sessionQueue, ^{
switch (self.setupResult) {
case AVCamSetupResultSuccess: {
// Only setup observers and start the session running if setup succeeded.
[self addObservers];
[self.session startRunning];
self.sessionRunning = self.session.isRunning;
break;
}
}
- (void)viewDidDisappear:(BOOL)animated {
dispatch_async(self.sessionQueue, ^{
if (self.setupResult == AVCamSetupResultSuccess) {
[self.session stopRunning];
[self removeObservers];
}
});
[super viewDidDisappear:animated];
}