Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the nicest way to do observer/observable in objective-c (iphone version)

I'm used to coding Java Swing UIs, and in those if you have some properties that change, and you want your UI to update, you would implement the observer/observable pattern. In Java you do this normally by having your class maintain a list of listeners that it notifies of different events.

I've played with Objective-C on the Mac, and that has KVC and binding which seems to work very nicely, and requires less code. The iPhone SDK doesn't seem to have this functionality though, so my question is: If I have a class that holds data that changes, what's the best way for me to register a UI component with that class so that it can be notified of changes in the data that it needs to display?

like image 866
rustyshelf Avatar asked Oct 03 '08 05:10

rustyshelf


People also ask

What is KVO in Objective C?

Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects. It is particularly useful for communication between model and controller layers in an application.

What is difference between Observable and observer in Android?

An observable object can have one or more observers. An observer may be any object that implements interface Observer . After an observable instance changes, an application calling the Observable 's notifyObservers method causes all of its observers to be notified of the change by a call to their update method.

What is Observer pattern in Swift?

Observer is a behavioral design pattern that allows some objects to notify other objects about changes in their state. The Observer pattern provides a way to subscribe and unsubscribe to and from these events for any object that implements a subscriber interface.


2 Answers

There are two built-in ways of doing observation in Cocoa: Key-Value Observing and notifications. In neither system do you need to maintain or notify a collection of observers yourself; the framework will handle that for you.

Key-Value Observing (KVO) lets you observe a property of an object — including even a property that represents a collection — and be notified of changes to that property. You just need to send the object -addObserver:forKeyPath:options:context: passing the object you want to receive updates, the key path of the property (relative to the receiver) for which you want to receive updates, and the types of updates you want to receive. (There are similar methods you can use if you want to observe a property representing a collection.)

Notifications are older and heavier-weight. You register with an NSNotificationCenter — usually the default center — an object and selector pair to be passed a notification when an event occurs. The notification object itself can contain arbitrary data via its userInfo property, and you can choose to observe all notifications of a specific name rather than those that apply to a particular object.

Which should you use in any particular case? In general, if you care about changes to a specific property of a specific object, use Key-Value Observing. That's what it's designed for and it's intentionally lightweight. (Among other uses, it is the foundation on which Cocoa Bindings are built.) If you care about a change in state that isn't represented by a property, then notifications are more appropriate.

For example, to stay in sync when the user changes the name of a model object, I'd use KVO. To know when an entire object graph was saved, I'd use notifications.

like image 189
Chris Hanson Avatar answered Sep 23 '22 05:09

Chris Hanson


I also found that you can do:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleWhateverChange) name:@"whateverChange" object:nil];

To register for change events, and

[[NSNotificationCenter defaultCenter] postNotificationName:@"whateverChange" object:nil];

To fire them. I might be a N00b but I just couldn't get the observer for key path thing to work for me.

like image 23
rustyshelf Avatar answered Sep 26 '22 05:09

rustyshelf