Skip to content
Simple, modern, thread-safe key-value observing for iOS and OS X.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Examples
FBKVOController.xcodeproj
FBKVOController.xcworkspace
FBKVOController Fix build error in Xcode 9b1 (#130) Jun 21, 2017
FBKVOControllerTests keyPath in FBKVONotificationBlock's change dictionary (#114) Aug 22, 2016
.gitignore
.travis.yml
CONTRIBUTING.md
KVOController.podspec
LICENSE improvements to README and CONTRIBUTING Feb 25, 2014
PATENTS
Podfile
Podfile.lock Use latest CocoaPods. Mar 11, 2017
README.md Updating ReadMe file to add important information about Swift (#120) Mar 3, 2017
Rakefile
codecov.yml Add CodeCov configuration. May 16, 2016

README.md

KVOController

Build Status Coverage Status Version Platform

Key-value observing is a particularly useful technique for communicating between layers in a Model-View-Controller application. KVOController builds on Cocoa's time-tested key-value observing implementation. It offers a simple, modern API, that is also thread safe. Benefits include:

  • Notification using blocks, custom actions, or NSKeyValueObserving callback.
  • No exceptions on observer removal.
  • Implicit observer removal on controller dealloc.
  • Thread-safety with special guards against observer resurrection – rdar://15985376.

For more information on KVO, see Apple's Introduction to Key-Value Observing.

Usage

Example apps for iOS and OS X are included with the project. Here is one simple usage pattern:

// create KVO controller with observer
FBKVOController *KVOController = [FBKVOController controllerWithObserver:self];
self.KVOController = KVOController;

// observe clock date property
[self.KVOController observe:clock keyPath:@"date" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew block:^(ClockView *clockView, Clock *clock, NSDictionary *change) {

  // update clock view with new value
  clockView.date = change[NSKeyValueChangeNewKey];
}];

While simple, the above example is complete. A clock view creates a KVO controller to observe the clock date property. A block callback is used to handle initial and change notification. Unobservation happens implicitly on controller deallocation, since a strong reference to the KVOController is kept.

Note: the observer specified must support weak references. The zeroing weak reference guards against notification of a deallocated observer instance.

NSObject Category

For an even easier usage, just #import <KVOController/NSObject+FBKVOController.h> for an automatic KVOController property on all objects.

[self.KVOController observe:clock keyPath:@"date" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew action:@selector(updateClockWithDateChange:)];

Swift

KVOController works great in Swift but there are few requirements:

  • Your observer should subclass NSObject.
  • Properties that you observe must be marked as dynamic.

Check the following example:

class TasksListViewModel: NSObject {

  dynamic var tasksList: [TaskList] = []
}

/// In ViewController.swift

import KVOController

kvoController.observe(viewModel,
                      keyPath: "listsDidChange",
                      options: [.new, .initial]) { (viewController, viewModel, change) in
    
  self.taskListsTableView.reloadData()
}

Prerequisites

KVOController takes advantage of recent Objective-C runtime advances, including ARC and weak collections. It requires:

  • iOS 6 or later.
  • OS X 10.7 or later.

Installation

To install using CocoaPods, add the following to your project Podfile:

pod 'KVOController'

To install using Carthage, add the following to your project Cartfile:

github "facebook/KVOController"

Alternatively, drag and drop FBKVOController.h and FBKVOController.m into your Xcode project, agreeing to copy files if needed. For iOS applications, you can choose to link against the static library target of the KVOController project.

Having installed using CocoaPods or Carthage, add the following to import in Objective-C:

#import <KVOController/KVOController.h>

Testing

The unit tests included use CocoaPods for managing dependencies. Install CocoaPods if you haven't already done so. Then, at the command line, navigate to the root KVOController directory and type:

pod install

This will install and add test dependencies on OCHamcrest and OCMockito. Re-open the Xcode KVOController workspace and Test, ⌘U.

License

KVOController is released under a BSD License. See LICENSE file for details.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.