UI Operation

UIViewController has functionality for presenting other view controllers. This notion is wrapped into UIOperation. It is mostly useful as a building block for other presentation operations such as AlertOperation, but can be used directly inside a view controller too.

Wrapping view controller presentation inside and Operation is useful for two reasons

  1. We can add mutual exclusivity, which prevents other view controller presentations from occurring. This may sounds trivial, but given the asynchronous nature of application, it is not that unlikely that this could happen.
  2. We can attach observers to clean up or tear down the current UI. This might mean removing selection highlighting or freeing up memory.

UIOperation is generic over two types. The controller being presented, which must be a UIViewController and the controller doing the presenting, which must conform to the PresentingViewController protocol. Operations framework adds conformance to UIViewController for this protocol.

The PresentingViewController protocol and ViewControllerDisplayStyle enum expresses the various methods for presenting one controller from another. Essentially, .Show, .ShowDetail and .Present.

For example, let assume we must present a login controller from our root controller:

let ui = UIOperation(
    controller: loginController, 
    displayControllerFrom: .Present(from)
)
queue.addOperation(ui)

Alternatively, maybe we have a split view controller and need to set the detail controller:

let show = DisplayDetailsViewController(from: .ShowDetail(self))
queue.addOperation(show)