Book Image

Objective C Memory Management Essentials

Book Image

Objective C Memory Management Essentials

Overview of this book

Table of Contents (18 chapters)
Objective-C Memory Management Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

When to avoid KVC and KVO


KVC and KVO, which we covered previously in Chapter 7, Key-value Programming Approaches, seems like a great mechanism for notifications at a very granular level, but it is possible to go wrong with KVO if you use it incorrectly. The removeObserver method will crash if you are not the observer for that key path, so keeping an exact track of the properties that you are observing is a must.

KVO only has one callback method. If you have multiple notifications, you need to handle them within one callback method, which makes your code inelegant and clunky like this:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  if ([keyPath isEqualToString:@"mySize"])
  {
            //Do something else
      }
   else if ([keyPath isEqualToString:@"anotherSize"])
   {
    //Do something else
  }
}

With a few more notifications, you will write a lot of if-else statements and you will be able to see how unwieldy...