Application Architecture for SwiftUI & Firebase

SwiftUI: Mapping Firestore Documents using Swift Codable

Peter Friese
Firebase Developers

--

Learn how to easily map Firestore data in Swift

Last time, we looked at how to connect a SwiftUI app to a Firebase project and synchronise data in real time. If you take a look at the code we used for mapping from Firestore documents to our Swift model structs, you will notice that is has a number of issues:

  • It is rather verbose — even for just the few attributes we’ve got!
  • The code makes some assumptions about the structure of the documents, such as the attribute types
  • Things might start breaking if we change the structure of the documents or the struct holding our model

So in this post, I’m going to show you how to make your mapping code more concise, less error-prone, and more maintainable by using the Codable protocol.

What is Codable?

The Codable protocol is Apple's contribution to a standardised approach to encoding…

--

--