Introducing Pinball

A small (and lightweight) networking library for Swift with Combine

Aaron Wright
2 min readDec 9, 2019
Photo by Thomas Millot on Unsplash

At WWDC 2019, Apple announced a new framework called Combine as a new way of dealing with asynchronous events in Swift. As of macOS 10.15 and iOS 13, Combine is now deeply integrated in many areas of the Foundation framework including Timer, NSNotification, and URLSession.

The Combine framework adds two new methods to URLSession for using a Combine Publisher:

func dataTaskPublisher(for request: URLRequest) -> URLSession.DataTaskPublisherfunc dataTaskPublisher(for url: URL) -> URLSession.DataTaskPublisher

While using a Publisher with Combine andURLSessionreally improves consuming a URL endpoint’s response data, it does not remove the pain of initializing and building a URLRequest. This was my motivation behind creating Pinball, a lightweight networking library for reducing the pain of representing URL endpoints with URLRequest.

In essence, Pinball is a lightweight wrapper around URLRequest with helpers for using URLSession. Unlike other heavier networking libraries like AlamoFire, Pinball makes no assumptions about how you would like to use the data you have received from a request and provides no help with tasks such as data encoding or decoding. This is intentional to keep the framework as small as possible. It currently weighs in a just 320 lines of Swift in only one Swift file!

Pinball trades the creation of a URLRequest for a Pinball.Endpoint:

Creating an endpoint

The endpoint can then be consumed and the data fetched by passing it to one of theURLSession helper methods that Pinball provides:

Consuming an endpoint using URLSession

That’s it! If you need a lightweight easy-to-use networking library for your next Swift project consider giving Pinball a try!

Follow me on Twitter and ask questions related to this post.

--

--