Read this before learning Server Side Swift

Diego Castro
11 min readApr 5, 2022

--

Co-authors: Galina Aleksandrova, Antonio Scognamiglio

Intro

Broadly speaking, along the path of every IOS developer comes the time to decide if they want to focus on FrontEnd development, BackEnd development, or design. Of course, it’s always desirable to have notions on all three of them, but at least at the beginning of the path, it’s complicated to even get to understand those concepts. BackEnd development is especially difficult to manage, since it’s related to a bunch of other branches of research, and sometimes it can reach a level of abstraction that is difficult to manage. That’s why here, we decided to discuss some topics that every IOS developer should be aware of before launching themselves towards the use of databases, API’s, Vapor and other tools related to Server Side Swift.

About Routing

In API and Web development, routing is the process of using URIs to control the user interface (UI). But, then, what’s a URI? Well, It stands for Uniform Resource Identifier, and it basically describes a specific location where the user can find the requested information. A URI may take the form of a Uniform Resource Locator (URL) or a Uniform Resource Name (URN), but nowadays a URI is almost always a URL. Anyway, the URI is a location, but, a location inside what? In this case, the location is inside a server, which may be internal or external. A server is basically a machine that stores information and keeps it accessible for other machines; servers usually store huge amounts of data, and therefore it is necessary to account with a team of backend developers capable of handling the data and keeping it available for users, without the need of absurd amounts of time to look for the requested information. For IOS developers, Vapor is the most common framework to design apps that require fetching data from the web. It is tightly integrated with Fluent, a framework that provides an easy-to-use interface to manage databases; this is important because the information is arranged inside servers as a database.

A bit about Vapor

Vapor is a useful and easy-to-use tool that allows developers to link their apps with internal or external servers; this is useful for apps that require a continuous supply of data, such as video and audio players, weather and finance related apps, and pretty much anything that is required to be connected to an external database. In order to understand a bit more about the way vapor works, one must first understand the way in which Swift manages the distribution of SwiftCode, which is through a tool know as Swift Package Manager (SPM). SPM leverages third party code by means of Package dependencies.

Package Dependencies

  • Swift Package Manager

Xcode comes with the Swift Package Manager (SPM). Since Vapor relies heavily on SPM, it’s a good idea to understand the basics of how it works. The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

  • What is a Package Dependency?

Sometimes writing everything from scratch is not a good solution, perhaps the code is complex, perhaps is easy to get wrong, and so on. Dependencies allow you to pull in third-party code, someone else’s written into your projects and use it there and you can save a lot of time this way.

Vapor’s list of pachage dependencies

What is a Package?

The Package is a directory that contains a Swift Package manifest. The manifest is a file called Package.swift, and it identifies that directory as a Swift Package. It also contains sources and of course, it contains unit tests to make sure that those sources continue functioning well. Underneath the sources is a subdirectory for each of the separate targets in the package. These are the separately buildable components of the package.

How can we use it?

It’s very easy to use; the first step is to go to Xcode’s file menu and click on “add packages”. Next, you will see a popup with a search bar on the top where you should introduce the URL of a git repository that contains the package you are looking for. You can also specify which version to download, this is particularly useful because it means that if the remote code changes somehow in the future, it won’t break your project. Finally, you just need to click the “Copy dependency” button and Xcode will start the download. Once it’s finished, the package will be listed in the project navigator. Now, you can go ahead and start using it just by typing “import” + “the name of the package”. Furthermore, since SPM is a very powerful tool, so autocompletion should work just fine.

First step for adding a package
Copying a new dependency

In order to download data from the web, either through github in the form of a Package, or through an API (more information below), it’s convenient to understand a few basics about HTTP requests, and its relationship with the structure of a URI. A URI is an identifier of the information that the user is looking for. It is typically formed by 3 or 4 elements, to say: a scheme, a host, a path, and a query string (<HTTP METHOD> <Hostname> <request-uri> <HTTP-VERSION>).

The structure of a URL
  1. The scheme specifies the protocol to be used to access the resource, it may be HTTP with or without SSL. HTTP stands for HyperText Transfer Protocol. This protocol allows the transference of information through archives in a request-reply fashion. For this purpose, it accounts with a series of methods that allow to communicate the type of action that the user wants the server to take. Actions may include: a request of information (GET), the input of information in the database (Create), replacing information in the database (PUT), updating information (PATCH), and deleting information (DELETE).
  2. The Hostname is the registered identification string that defines the website location, it may be a word or a phrase. It may also be used for marketing purposes. A web host is basically the name of the device where the files related to a web page are stored. The client can use the IP address of the server instead of the host name, but that’s highly unusual since it is easier to remember hostnames. In the case of the URL “https://www.amazon.com”, <www> is the subdomain, <com> represents the top level domain, and <amazon> is the second level domain. The three of them account for the host name.
  3. The path, just as the name says, specifies the resource in the host that the client wants to access. The path comes right after the host name, starting with a /; each field of the path is also separated by /.
  4. The query string is optional, it provides additional information that the source can use for a specific purpose.

After the path, URI’s usually include the HTTP that the client is using. This is useful because that way the server can know how to interpret the request and the correct way to return the requested information. Most of the time, the data that a developer gets from internet is obtained by means of an API, a systems that basically serves as a bridge between the requester of data and the database.

API

What is an API?

API is the acronym of Application Programming Interface, is simply some software that sends information back and forth between a website or app and a user. APIs are important because, although we don’t always notice them, they are everywhere on the web. Imagine, for example, a person that wants to share an Amazon article on their webpage or app; it will be kind of messy to create a template, take pictures and keep monitoring for the price of this article. Now, imagine a case in which there is not only one product but several hundreds of products. In this case, there is not enough to do the work by hand. So, in this case, there is the alternative to use the Amazon API to get updated information from the Amazon servers simply by means of an HTTP request. The situation is pretty much the same for the people that manage weather and finance apps, maps, and pretty much everywhere.

Testing API with Postman

Now, in web and app development, there’s always the need to test if we are getting the right information from the server. At a first glance, it may sound complicated, because a lot of things can go wrong and it is sometimes messy to get to know which part of the process went wrong. Luckily, there are a bunch of tools that allow us to test if an API is working properly. One of the favourites is Postman, a tool that, besides displaying a very easy-to-use interface, allows users to test if a specific HTTP method is working properly, for example, GET, POST, PUT, PATCH, or DELETE. Also, it is especially useful because it allows users to check the type of data that they are getting from the server. Users just need to paste the URL that they want to test on Postman, and they will get a series of tools to analyze the response. Insomnia is a very good option for those users that, for any reason, choose not to use Postman.

This is a response of a GET request.
In this snippet you can see a POST request with a JSON file in its body.

So, now you know how to create HTTP requests and retrieve your information. But where to store this information? In the Database!

And what is a Database?

A database is an organized collection of structured information or data.

A database is controlled by a database management system (DBMS) — a database software program, which is basically just an interface between users and the database and it allows users to retrieve, update, organize and manage information in the database.

There are two main types of databases, depending on how it stores the data

  • Relational database — it’s a database that stores and provides access to data points that are related to one another. In a relational database, each row in the table is a record with a unique ID called the key. The columns of the table hold attributes of the data, and each record usually has a value for each attribute, making it easy to establish the relationships among data points.
➡️ Example of a relational database.
  • Non-relational database (or NoSQL Database) ****stores data not in tables, instead it stores in multiple ways (Key-value, Document-based, Column-based).
Exmaples of non-relational databases

Other database types depend on how its managed

  • Object-oriented databases is a database, managed by an object-oriented database management system (OODBMS). The database combines object-oriented programming concepts with relational database principles.
  • Distributed databases. These kind of databases consist of two or more files located in different sites. The database may be stored on multiple computers, located in the same physical location, or scattered over different networks.
  • Embedded database is a database technology in which database management solutions are built into an application rather than provided as standalone tools. In many cases, this effectively “hides” the database management tools from the end-user.

And many more. But the most important thing you need to remember is how a relational database stores its data (in tables) and that a non-relational database can store the data in multiple ways.

Relational databases use structured query language (SQL) for writing and querying data. SQL is a programming language used by nearly all relational databases to query, manipulate, and define data, and to provide access control.

In simple words, databases are very familiar to spreadsheets (like excel) and use SQL to change smth in that “spreadsheet”.

When we read documentation we can often see the word CRUD. But what does it mean? Simply — Create, read, update and delete. It’s a four basic operation of persistent storage any data storage device that retains data after power to that device is shut off).

Fluent and Vapor

Fluent is Vapor’s object-relational mapping framework. It’s an abstraction layer between the Vapor and the database.

Fluent simplifies your life because you don’t need to know how to write database queries (which are all strings and not type-safe like in swift). Fluent allows you to use several database engines, even in the same app. Fluent creates model types that represent data structures in your database. These models are then used to perform CRUD operations, instead of writing raw queries.

But what is an object-relational mapping tool? Read here.

Database engines that fluent support:

  1. Postgres (Recommended)

Is an open source, object-relational SQL database. It is easily configurable on most cloud hosting providers.

  1. MySQL

Is a popular open source SQL database. It is available on many cloud hosting providers, and also supports MariaDB.

  1. SQLite

Is an open source, embedded SQL database. Its simplistic nature makes it a great candidate for prototyping and testing.

  1. MongoDB

Is a popular schemaless NoSQL database designed for programmers. The driver supports all cloud hosting providers and self-hosted installations from version 3.4 and up.

Models are used to represent data stored in tables or collections in your database. Models have one or more fields that store codable values.

Conclusion

Server side Swift is one of those topics that takes a lot of pages and time to discuss, however, an exhaustive review of it is out of the scope of this article. Anyway, that’s shouldn’t be a motive to decrease your curiosity about this field. If we did our work correctly, by now you should have at least a small notion of how convenient it is to know a bit more about Routing, the structure of a URL (both concepts are deeply linked), how useful are APIs for developers, and how Vapor makes the life of coders a bit easier by giving them the means to links their Swift based apps to external data bases. It’s a great start! but you should not stop here, if you hanger of knowledge about server side swift has only increased, we highly recommend you to check the following reading list, specially created for beginners.

--

--

Diego Castro

I am a former genetic engineer currently studying App develpment at the Apple deloper academy in Naples