1

I want to separate the functionality of adding an observer and its removal from the view controller. Currently, I am adding observer in the viewWillAppear() method and remove the observer in the viewWillDisappear(). Now, I want to have light view controller separate out the observer code from this view controller to some other class. I think I need to create a new class and set this view controller as the new class delegate but I am not sure on this. I would really appreciate if someone could help me how to do this thing.

Utsav Dusad
  • 2,139
  • 4
  • 31
  • 52

1 Answers1

2

What do you mean by Observer? NSNotificationCenter? Maybe a code snippet might make it clearer?

FWIW...viewWillAppear() and viewWillDisappear() can be called more than once per view lifetime so just bear that in mind. In this scanerio it should be fine, so long as they are balanced.

I typically add/remove observers in init/dealloc (Objc-C) or init/deinit (Swift) and use isViewLoaded to ensure I do not touch the UI when it's not there but it's up to you which you prefer.

For your actual observer pattern, I would, indeed, break out a new class, for handling the observer callbacks. If, however, the response to the observer callbacks is to update the user interface of the view controller, then you have some separation of concerns challenges. Probably the best thing would be for the view controller to spin up some new class that automatically listens for the NSNotificationCenter event(s) on init and removes itself on dealloc. Then, define a simple @required protocol as a weak delegate of the class. Your view controller can create this object (or, better for testing, get it dependency injected in at init), conform to that protocol and assign itself as the delegate.

Something like

@protocol MyObservable {
@required
    -(void)myXYZEventDidOccur;
}

@interface MyObserver : NSObject {

-(instancetype)initWithDelegate:(id<MyObservable>)delegate;

}

@interface MyObserver ()
    @property (nonatomic, weak, readwrite, nullable) id<MyObservable>delegate;
}

@implementation MyObserver {

    -(instancetype)initWithDelegate:(id<MyObservable)delegate {
        self = [super init];
        if(self == nil) { return nil; }

        self.delegate = delegate;

        [NSNotificationCentre defaultCentre] addObserver...];
    }

    -(void)dealloc {
        [NSNotificationCentre defaultCentre] removeObserver...];
    }

    -(void)didReceiveNotificationXYZ:(NSNotification *)notification {
        [self.delegate myXYZEventDidOccur];
    }
}

To get really smart, you can not even bother observing until you have a delegate set. Similarly, stop observing when there is no delegate.

Michael
  • 1,213
  • 6
  • 9
  • 1
    Yes, NSNotificationCenter. I understand that observer will be created and deallocated when viewWillAppear() or ViewWillDisappear() is called. Doing so allows me to release the memory for observer when I get out of the view. And Isn't the dealloc() can't be used when ARC is enabled. – Utsav Dusad Apr 03 '16 at 12:30
  • dealloc is perfectly legitimate in an ARC world, but the way you use it is subtly changed. See [dealloc](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/index.html#//apple_ref/occ/instm/NSObject/dealloc) for details but basically, do not use it for managing limited resources and do not call super. – Michael Apr 03 '16 at 19:25
  • 1
    Can you describe in which situations viewWillDisappear() and viewWillAppear() breaks the code. I checked few links which suggested to use these methods to add or remove the observer. See Here: [Link1](http://www.daveoncode.com/2011/12/12/quick-ios-tip-where-is-the-right-place-to-add-and-remove-observers-in-uiviewcontrollers/), [Link2](http://stackoverflow.com/questions/10631375/whats-the-better-way-to-addobserver-removeobserver-with-nsnotificationcenter). – Utsav Dusad Apr 04 '16 at 14:44
  • You know, maybe this isn't a thing anymore, and maybe I am leading you down a bad road. I cannot find references to it either. Gonna delete those comments until I can find empirical evidence. Really sorry. – Michael Apr 05 '16 at 13:51