The Solution

A network operation is a discrete action and should be coded as such. Therefore, we’ll use a subclass of NSOperation as the basis for the network operation.

Instead of using the block-based API provided by Apple, we’ll use the delegate functions of NSURLSession to listen to events during the network operation. By implementing the delegate methods, our NSOperation subclass will be integrated into every step of the network request and can make decisions through the operation.

The workflow is as follows:

  1. Build the request
  2. Start network operation
  3. Receive a response from the server
  4. Receive N blocks of data from the server
  5. Receive a completion delegate callback
  6. Process the data

The start() Function

In our NSOperation subclass, we’ll handle ...

Get Core Data in Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.