MongoDB Developer
Realm
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Realmchevron-right

Realm-Swift Type Projections

Andrew MorganPublished Jan 26, 2022 • Updated May 09, 2022
RealmSwift
Copy Link
facebook icontwitter iconlinkedin icon
random alt
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

Realm natively provides a broad set of data types, including Bool, Int, Float, Double, String, Date, ObjectID, List, Mutable Set, enum, Map, …
But, there are other data types that many of your iOS apps are likely to use. As an example, if you're using Core Graphics, then it's hard to get away without using types such as CGFloat, CGPoint, etc. When working with SwiftUI, you use the Color struct when working with colors.
A typical design pattern is to persist data using types natively supported by Realm, and then use a richer set of types in your app. When reading data, you add extra boilerplate code to convert them to your app's types. When persisting data, you add more boilerplate code to convert your data back into types supported by Realm.
That works fine and gives you complete control over the type conversions. The downside is that you can end up with dozens of places in your code where you need to make the conversion.
Type projections still give you total control over how to map a CGPoint into something that can be persisted in Realm. But, you write the conversion code just once and then forget about it. The Realm-Swift SDK will then ensure that types are converted back and forth as required in the rest of your app.
The Realm-Swift SDK enables this by adding two new protocols that you can use to extend any Swift type. You opt whether to implement CustomPersistable or the version that's allowed to fail (FailableCustomPersistable):
In this post, I'll show how the Realm-Drawing app uses type projections to interface between Realm and Core Graphics.

Prerequisites

The Realm-Drawing App

Realm-Drawing is a simple, collaborative drawing app. If two people log into the app using the same username, they can work on a drawing together. All strokes making up the drawing are persisted to Realm and then synced to all other instances of the app where the same username is logged in.
Animated screen captures showing two iOS devices working on the same drawing
It's currently iOS-only, but it would also sync with any Android drawing app that is connected to the same Realm back end.

Using Type Projections in the App

The Realm-Drawing iOS app uses three types that aren't natively supported by Realm:
  • CGFloat
  • CGPoint
  • Color (SwiftUI)
In this section, you'll see how simple it is to use type projections to convert them into types that can be persisted to Realm and synced.
Realm Schema (The Model)
An individual drawing is represented by a single Drawing object:
A Drawing contains a List of Line objects:
It's the Line class that uses the non-Realm-native types.
Let's see how each type is handled.
CGFloat
I extend CGFloat to conform to Realm-Swift's CustomPersistable protocol. All I needed to provide was:
  • An initializer to convert what's persisted in Realm (a Double) into the CGFloat used by the model
  • A method to convert a CGFloat into a Double:
The view can then use lineWidth from the model object without worrying about how it's converted by the Realm SDK:
CGPoint
CGPoint is a little trickier, as it can't just be cast into a Realm-native type. CGPoint contains the x and y coordinates for a point, and so, I create a Realm-friendly class (PersistablePoint) that stores just that—x and y values as Doubles:
I implement the CustomPersistable protocol for CGPoint by mapping between a CGPoint and the x and y coordinates within a PersistablePoint:
SwiftUI.Color
Color is made up of the three RGB components plus the opacity. I use the PersistableColor class to persist a representation of Color:
The extension to implement CustomPersistable for Color provides methods to initialize Color from a PersistableColor, and to generate a PersistableColor from itself:
The view can then use selectedColor from the model object without worrying about how it's converted by the Realm SDK:

Conclusion

Type projections provide a simple, elegant way to convert any type to types that can be persisted and synced by Realm.
It's your responsibility to define how the mapping is implemented. After that, the Realm SDK takes care of everything else.
Please provide feedback and ask any questions in the Realm Community Forum.

Copy Link
facebook icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
News & Announcements

New Realm Data Types: Dictionaries/Maps, Sets, Mixed, and UUIDs


Oct 19, 2022
News & Announcements

Realm Cocoa 5.0 - Multithreading Support with Integration for SwiftUI & Combine


Oct 19, 2022
Tutorial

Realm Data and Partitioning Strategy Behind the WildAid O-FISH Mobile Apps


Sep 23, 2022
Article

SwiftUI Best Practices with Realm


Oct 19, 2022
Table of Contents