Giter Site home page Giter Site logo

connews's Introduction

ConNews

A small demo app demonstrating the use of NSOperationQueue in Swift to parallelize network requests for a better user experience. Some features of Grand Central Dispatch are also utilized.

This app was created to accompany a conference talk entitled: "Threads, Queues, and Things to Come; The Present and Future of Concurrency in Swift". This talk will be presented at 360iDev in Denver, CO on Tuesday, August 15, 2017.

What is the app?

This demo app implements a simple HackerNews client. The HackerNews API provides an endpoint for loading the list of top 500 stories on the site. It returns a list of unique Ids. Each Id must be used in another endpoint to fetch the story data itself. Additionally, the app shows the Favicon of each site next to the story. The path of the icon must be derived from the data fetched with the story.

What does it demonstrate?

Several useful features of NSOperationQueue are demonstrated:

  1. Basic Queueing
  2. Dependent Operations
  3. Pausing/Resuming a Queue

Additionally, Grand Central Dispatch (GCD) is utilized to:

  1. Protect Shared Mutable State
  2. Serialize a Network Callback

NSOperationQueue

NSOperationQueue is an Objective-C class that bridges to Swift as simply OperationQueue. It is a powerful tool for queuing work to be done asynchronously without much effort. In this demo, we demonstrate:

Basic Queuing

Each request to fetch a story is implemented as an NSOperation and added to an NSOperationQueue. As each request completes, its data is used to update the UI.

Dependent Operations

Fetching the Favicon for each website is added as a dependency of the story fetch. It utilizes the data from the story to derive the path to the Favicon.

Pausing/Resuming a Queue

If the user taps a cell, to load a story in SafariViewController, we pause any stories and icons that are still loading in the background.

Grand Central Dispatch

Grand Central Dispatch, called GCD or sometimes libdispatch, is a C library which bridges into Swift as normal types. While slightly more cumbersome than NSOperationQueue, it is still powerful and awesome useful in tandem with a full blown operation queue. In this demo, we do just that, using GCD to:

Protect Shared State

A serial Grand Central Dispatch queue is used to protect access to the shared list of stories as each fetch request completes. Mutating an Array in Swift is not an atomic operation. A serial queue can be used much he same way a traditional Lock or call to @synchronize would be in Objective-C.

Serialize a Network Callback

Our fetch request occurs in the main() method of our NSOperation subclass. We don't want the operation to return until the request is complete. We use a GCD DispatchGroup to block the operation until the data has been acquired.

Resources

Go deeper in your understanding of concurrency in Swift.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.