Giter VIP home page Giter VIP logo

autoremoveobserver's Introduction

AutoRemoveObserver

The problem with NotificationCenter and KVO observers is that you have to remember to remove the observing object when it gets dealloc'ed or your app will crash on the next notification.

The methods below create a small "remover" object that is associated with the observer object such that when the observer object gets dealloc'ed this object will also get dealloc'ed and remove the observations.

N.B.: This class is designed to remove the observer when the observer is dealloc'ed. If you need to remove the observer before that then you should use the standard methods and be sure to handle the dealloc case yourself.

Instead of writing:

	// Selector based
	[[NSNotificationCenter defaultCenter] addObserver:notificationObserver
											 selector:notificationSelector
												 name:notificationName
											   object:notificationSender];

	// Block based
	[[NSNotificationCenter defaultCenter] addObserverForName:name
													  object:obj
													   queue:queue
												  usingBlock:block];

	// KVO
	[self addObserver:anObserver forKeyPath:keyPath options:options context:context];

you write:

	// Selector based
	[INTUAutoRemoveObserver addObserver:notificationObserver
							   selector:notificationSelector
								   name:notificationName
								 object:notificationSender];

	// Block based
	[INTUAutoRemoveObserver addObserver:self
								forName:name
								 object:obj
								  queue:queue
							 usingBlock:block];

	// KVO
	[INTUAutoRemoveObserver addObserver:anObserver
							 forKeyPath:keyPath
								options:options
								context:context
					   onReceiverObject:self];

E.g.:

	// Selector based
	[INTUAutoRemoveObserver addObserver:self
							   selector:@selector(myMethod)
								   name:@"BroadcastNotification"
								 object:nil];

	// Block based
	MyObject* __weak weakSelf = self;		// So the block doesn't retain us
	[INTUAutoRemoveObserver addObserver:self
								forName:@"BroadcastNotification"
								 object:nil
								  queue:[NSOperationQueue mainQueue]
							 usingBlock:^(NSNotification *note) {
										 [weakSelf observeMe];
								 }];

	// KVO
	[INTUAutoRemoveObserver addObserver:self		// self or some observer object. When this is dealloc'ed then observer is removed.
							 forKeyPath:NSStringFromSelector(@selector(myProperty))
								options:NSKeyValueObservingOptionNew
								context:nil
					   onReceiverObject:self];

	N.B.: If the observer is self you will see a error in the log file about the instance being deallocated while key
	observers were still registered to it (even though they are removed as a side effect of self getting dealloc'ed)
	when self is dealloc'ed.

autoremoveobserver's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.