Swift/UnwrapDiscover about Swift, iOS and architecture patterns

State of the Art 2021

December 28, 2021

2021 is almost over. It's a perfect time to have quick look on the technologies I've been using along the year and where the project I'm working on stand.

Tech stack

I have the huge opportunity to work on a SwiftUI project. So my tech stack is quite modern:

  • 🎨 SwiftUI
  • 🔀 Combine
  • 🛠 Xcode 12.x
  • 📦 SPM

SwiftUI

Most of the interface is developed using SwiftUI. It's a delight using this technology.

However while coding quicker and having less conflicts in our pull requests we faced some unexpected UI bugs due to the framework maturity. That's probably the biggest concern about SwiftUI for now: too many bugs, not as trustworthy as UIKit.

What about UIKit then? We use it a little bit when facing limitation with SwiftUI. Luckily for us we dropped iOS13 support in the middle of the year. We're now in process of migrating those few components in plain SwiftUI thanks to the latest features iOS14 give us access to (@StateObject, ScrollViewReader, ...).

Combine

Using SwiftUI we are de-facto using Combine for handling asynchronous calls. It work well although like with any other reactive library my main concern is the same: use it widely. I still see some of my teammates doing too complex, non readable reactive code. With great power comes great responsibility.

async/await should help easing some of our asynchronous code. Sadly we did not migrate yet to Xcode 13 so it will be for next year.

Xcode

As previously told we did not migrate to Xcode 13. We faced bugs while trying to compile our project with it and due to reported bugs decided to wait before using it. Xcode 13.2.1 might be the right one 🤷‍♂️

SPM

We exclusively use Swift Package Manager and dropped our last Carthage dependencies. It work pretty well despite a bug preventing compilation every time we switch branch (hopefully fixed in Xcode 13 🤞).

We're also very excited about upcoming features in next swift version!

Architecture

The application architecture did not change a lot throughout the year and we are mainly using:

  • 🧱 MVVM
  • 💾 CoreData

MVVM

Code is written following MVVM architecture. It work well with SwiftUI and we do not face any specific issue albeit not having injection. That's one of our main goal for 2022.

We also started migrating our views to Smart/Dumb component approach. We saw great improvements in our codebase:

  • Less stateful views and less views owning a ViewModel
  • Less views depending from a ViewModel

This turned into having less bugs 🐛💥

CoreData

Application data are stored locally using CoreData. And like 90% of the projects where I saw CoreData usage... that's pretty much useless 😉

So we're working on two things:

  1. Introducing a domain model to be less dependent from CoreData
  2. Removing CoreData usage when possible

All this stuff is not finished yet and more complex than I though because of lack of knowledge on domain model from some of my teammates. Next time I'll be more vigilant to not only explain what we're going to do but also how.

But we already see improvements:

  • We have less optionals so less edge cases
  • We have more expressive models so the code is easier to understand for newcomers
  • On global it better reflect the app functionalities/vocabulary

Environment

The environment is where most changes happened along the year. Our final stack is:

  • 🦊 Gitlab
  • 🚀 fastlane
  • 🤖 Bitrise

Gitlab

We used to use Bitbucket and was very happy to move to Gitlab over the year. We gained a better UI, labels for MR and a few other things.

In the meantime we lost some functionalities we did not expect:

  • No todo list on the MR
  • Squashing MR produce 2 commits: one squash commit AND one merge commit 😡

Fastlane

While moving from Bitbucket to Gitlab we decided to start using dotenv files to extract environment values and ease transition from one CI to another. Still some improvements we could make on 2022.

Bitrise

We changed our CI to Bitrise. Having used it before on many projects I was very happy to use it again. The tool is still easy to take in charge and migration was quite easy thanks to the few changes we made to our fastlane files.

That being said I was very disappointed on the new announced pricing. We're paying €400/month for 5 developers! Our build time being around 10-12min (so not very long) it seem over priced in my opinion.

Future direction

2021 was the year where we started a lot of subjects to stabilize our code base. 2022 should be the achievement year and see the app go to a next level:

  • 🛠 Xcode 13 migration
  • 🎭 Deprecation of our last UIKit components
  • 🏁 Domain model everywhere and CoreData drop
  • 💉 Dependency injection
  • 🚦 async/await