HomeLearnHow-toAdding Realm as a dependency to an iOS Framework

Adding Realm as a dependency to an iOS Framework

Updated: Jul 07, 2021 |

Published: Jun 29, 2021

  • Mobile
  • Realm
  • Swift
  • ...

By Diego Freniche

Rate this article

#Adding Realm as a Dependency to an iOS Framework

#Introduction

In this post we’ll review how we can add RealmSwift as a dependency to our libraries, using two different methods: Xcode assistants and the Swift Package Manager.

#The Problem

I have a little, nice Binary Tree library. I know that I will use it for a later project, and that it'll be used at least in a macOS and iOS app. Maybe also a Vapor web app. So I decided to create a Framework to hold this code. But some of my model classes there need to be persisted in some way locally in the phone later. The Realm Mobile library is perfect for this, as I can start working with regular objects and store them locally and, later, if I need a really simple & quick to implement backend solution I can use Realm Sync.

But the problem is, how do we add Realm as a dependency in our Frameworks?

#Solution 1: Use Xcode to Create the Framework and Add Realm with SPM

The first way to create the Framework is just to create a new Xcode Project. Start Xcode and select File > New > Project. In this case I’ll change to the iOS tab, scroll down to the Framework & Library section, then select Framework. This way I can share this Framework between my iOS app and its extensions, for instance.

Choosing a template for the project

Now we have a new project that holds our code. This project has two targets, one to build the Framework itself and a second one to run our Unit Tests. Every time we write code we should test it, but this is especially important for reusable code, as one bug can propagate to multiple places.

To add Realm/Swift as a dependency, open your project file in the File Navigator. Then click on the Project Name and change to the Swift Packages tab. Finally click on the + button to add a new package.

Adding Realm as a Package to the Project, step by step

In this case, we’ll add Realm Cocoa, a package that contains two libraries. We’re interested in Realm Swift: https://github.com/realm/realm-cocoa. We want one of the latest versions, so we’ll choose “Up to major version” 10.0.0. Once the resolution process is done, we can select RealmSwift.

Nice! Now that the package is added to our Framework we can compile our code containing Realm Objects without any problems!

#Solution 2: create the Framework using SPM and add the dependency directly in Package.swift

The other way to author a framework is to create it using the Swift Package Manager. We need to add a Package Manifest (the Package.swift file), and follow a certain folder structure. We have two options here to create the package:

  • Use the Terminal
  • Use Xcode

#Creating the Package from Terminal

  • Open Terminal / CLI
  • Create a folder with mkdir yourframeworkname
  • Enter that folder with cd yourframeworkname
  • Run swift package init
  • Once created, you can open the package with open Package.swift

Terminal showing the results of running above commands

#Creating the Package using Xcode

You can also use Xcode to do all this for you. Just go to File > New > Swift Package, give it a name and you’ll get your package with the same structure.

Xcode showing the newly created Package.swift

#Adding Realm as a dependency

So we have our Framework, with our library code and we can distribute it easily using Swift Package Manager. Now, we need to add Realm Swift. We don’t have the nice assistant that Xcode shows when you create the Framework using Xcode, so we need to add it manually to Package.swift

The complete Package.swift file

1let package = Package(
2 name: "BinaryTree",
3 platforms: [
4 .iOS(.v14)
5 ],
6 products: [
7 // Products define the executables and libraries a package produces, and make them visible to other packages.
8 .library(
9 name: "BinaryTree",
10 targets: ["BinaryTree"]),
11 ],
12 dependencies: [
13 // Dependencies declare other packages that this package depends on.
14 .package(name: "Realm", url: "https://github.com/realm/realm-cocoa", from: "10.7.0")
15 ],
16 targets: [
17 // Targets are the basic building blocks of a package. A target can define a module or a test suite.
18 // Targets can depend on other targets in this package, and on products in packages this package depends on.
19 .target(
20 name: "BinaryTree",
21 dependencies: [.product(name: "RealmSwift", package: "Realm")]),
22 .testTarget(
23 name: "BinaryTreeTests",
24 dependencies: ["BinaryTree"]),
25 ]
26)

Here, we declare a package named “BinaryTree”, supporting iOS 14

1let package = Package(
2 name: "BinaryTree",
3 platforms: [
4 .iOS(.v14)
5 ],

As this is a library, we declare the products we’re going to build, in this case it’s just one target called BinaryTree.

1products: [
2 // Products define the executables and libraries a package produces, and make them visible to other packages.
3 .library(
4 name: "BinaryTree",
5 targets: ["BinaryTree"]),
6 ],

Now, the important part: we declare Realm as a dependency in our library. We’re giving this dependency the short name “Realm” so we can refer to it in the next step.

1dependencies: [
2 // Dependencies declare other packages that this package depends on.
3 .package(name: "Realm", url: "https://github.com/realm/realm-cocoa", from: "10.7.0")
4 ],

In our target, we use the previously defined Realm dependency.

1.target(
2 name: "BinaryTree",
3 dependencies: [.product(name: "RealmSwift", package: "Realm")]),

And that’s all! Now our library can be used as a Swift Package normally, and it will include automatically Realm.

#Recap

In this post we’ve seen different ways to create a Framework, directly from Xcode or as a Swift Package, and how to add Realm as a dependency to that Framework. This way, we can write code that uses Realm and distribute it quickly using SPM.

In our next post in this series we’ll document this library using the new Documentation Compiler (DocC) from Apple. Stay tuned and thanks for reading!

If you have questions, please head to our developer community website where the Realm engineers and the Realm/MongoDB community will help you build your next big idea with Realm and MongoDB.

Rate this article
MongoDB Icon
  • Developer Hub
  • Documentation
  • University
  • Community Forums

© MongoDB, Inc.