Isolate and modularize your app’s business logic

Black and white photo of a person with a Macbook on their lap.
Black and white photo of a person with a Macbook on their lap.
Photo by Sergey Zolkin on Unsplash

In this article, we’ll learn about Operation dependencies and how they help us chain several operations, resulting in isolated and reusable business-logic components. We’ll chain two operations: one for loading an image and another for adding a sepia effect to it.

In short, this is what we’ll master:

This is the result we’ll have by the end of the tutorial:


You don’t need a third-party library for this

spinning wheel of bright neon lights set above a neon sign
spinning wheel of bright neon lights set above a neon sign
Photo by Kinson Leung on Unsplash

In this tutorial, we will learn how to implement a circular loading indicator in Swift. By the end, you will have a reusable UI element you can use in your apps to suit your needs.

In short, this is what we will master in this article:

This is the result we are going to achieve:


Leverage a built-in caching mechanism

Image for post
Image for post
Photo by Scott Webb on Unsplash.

Today, we will learn how to use NSCache in Swift to cache images inside a UICollectionView.

In short, this is what you will master in this tutorial:

This is what we will have at the end of this article:


Set the max number of operations to be executed at one time

Image for post
Image for post
Photo by Kevin Crosby on Unsplash.

In this article, we will learn how to limit the number of ongoing network requests using a DispatchSemaphore.

Limiting resource-heavy operations allows us to manage thread resources more efficiently. For example, when you have a task to simultaneously fetch ten high-quality images, it is useful to allow the execution of only one or two image loading operations at a time.

In short, this is what you will master in this tutorial:

Let’s Start

First, let’s create a URL, a background DispatchQueue, and a DispatchSemaphore with a value of…


Leverage the power of the .zip operator

Image for post
Image for post
Photo by Daniel Romero on Unsplash.

Consider this case: A specific screen in your app requires firing two different requests at the same time. Your goal is to update the UI only after both of them complete. In this tutorial, we will learn how to observe the completion of several network requests using Combine.

We will leverage a powerful .zip operator that allows us to receive a tuple containing events from several upstream Publishers.

In short, these are the topics you will master in this tutorial:


Using containers and child view controllers

Image for post
Image for post
Photo by Amirmahdi Kazemnejad on Unsplash

In this article, we will learn how to create a reusable UI element — a bottom sheet.

At the end of the tutorial, you will have a finished component you can easily copy and paste into your app and use to suit your needs.

This is what we are going to build:


Add support for right-to-left languages

Image for post
Image for post
Photo by Bruno Wolff on Unsplash

In this article we will learn about the difference between leading and left, trailing and right constraints in Swift.

In short, by setting the leading constraint we set the starting point of a view, while the trailing constraint sets the ending point. If we are using English language in our app, leading and left anchors are the same thing, just like the trailing and right ones. But if we add support for RTL (Right-to-left) languages like Arabic, Hebrew, or Urdu, leading will mean right, and trailing will mean left.

Now let’s explore this difference on a simple example.

Let’s Start

Our ViewController has a rounded UIView containing a…


A flexible solution to copy and paste into your new app

Image for post
Image for post
Photo by Thom Bradley on Unsplash

In this article, we will learn how to architect and create an efficient and easy to use networking layer by leveraging Combine publishers, Codable, CustomStringConvertible protocol, and a custom Endpoint struct.

At the end of the tutorial, you will have a ready-to-use implementation that you can easily copy and paste in your app and expand it the way you want.

The full source code is available at the bottom of the article.

Let’s Start

Before we dive into the code, let’s first outline all components of our networking layer:


Using a simple extension and a built-in publisher

Image for post
Image for post
Photo by William Hook on Unsplash.

In this tutorial, we will learn how to easily observe UserDefaults using a Combine publisher.

Without further ado, let’s get started.

Let’s Start

Consider a case where you want to persist a game’s music volume in UserDefaults. You also want to observe the change and react accordingly. So our first step is to define the following extension:

As we can see, the getter returns the Float value for the music_volume key and the setter sets the new value for that key.

Next, let’s import Combine and add the subscriptions property to store our future subscription:

Now we can add the actual subscription inside the viewDidLoad()


A handy extension to copy and paste in your app

Image for post
Image for post
Photo by freddie marriage on Unsplash

In this article, we will learn how we can easily automate debugging Swift objects using a handy CustomStringConvertible protocol conformance in an extension.

The source code of the extension is available at the bottom of the article.

Without further ado, let’s get started.

Let’s Start

If you have ever conformed to the CustomStringConvertible protocol, then this code looks very familiar to you:

About

Zafar Ivaev

 WWDC20 Scholar. I write about features of Swift, iOS development practices, and sometimes Flutter.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store