# Adapting Apple's Scrumdinger SwiftUI Tutorial App to Use Realm

Apple published a great tutorial to teach developers how to create iOS apps using SwiftUI. I particularly like it because it doesn't make any assumptions about existing UIKit experience, making it ideal for developers new to iOS. That tutorial is built around an app named "Scrumdinger," which is designed to facilitate daily scrum) meetings.

Apple's Scrumdinger implementation saves the app data to a local file whenever the user minimizes the app, and loads it again when they open the app. It seemed an interesting exercise to modify Scrumdinger to use Realm rather than a flat file to persist the data. This article steps through what changes were required to rebase Scrumdinger onto Realm.

An immediate benefit of the move is that changes are now persisted immediately, so nothing is lost if the device or app crashes. It's beyond the scope of this article, but now that the app data is stored in Realm, it would be straightforward to add enhancements such as:

-   Search meeting minutes for a string.
-   Filter minutes by date or attendees.
-   Sync data so that the same user can see all of their data on multiple iOS (and optionally, Android) devices.
-   Use Realm Sync Partitions to share scrum data between team members.
-   Sync the data to MongoDB Atlas so that it can be accessed by web apps or through a GraphQL API

>
>
>This article was updated in July 2021 to replace `objc` and `dynamic` with the `@Persisted` annotation that was introduced in Realm-Cocoa 10.10.0.
>
>

## Prerequisites

-   Mac (sorry Windows and Linux users).
-   Xcode 12.4+.

I strongly recommend that you at least scan Apple's tutorial. I don't explain any of the existing app structure or code in this article.

## Adding Realm to the Scrumdinger App

First of all, a couple of notes about the GitHub repo for this project:

-   The main branch is the app as it appears in Apple's tutorial. This is the starting point for this article.
-   The realm branch contains a modified version of the Scrumdinger app that persists the application data in Realm. This is the finishing point for this article.
-   You can view the diff between the main and realm branches to see the changes needed to make the app run on Realm.

### Install and Run the Original Scrumdinger App

``` bash
git clone https://github.com/realm/Scrumdinger.git
cd Scrumdinger
open Scrumdinger.xcodeproj
```

From Xcode, select a simulator:

    
    Select an iOS simulator in Xcode.

Build and run the app with `⌘r`:

    
    Scrumdinger screen capture

Create a new daily scrum. Force close and restart the app with `⌘r`.  Note that your new scrum has been lost 😢. Don't worry, that's automatically fixed once we've migrated to Realm.

### Add the Realm SDK to the Project

To use Realm, we need to add the Realm-Cocoa SDK to the Scrumdinger Xcode project using the Swift Package Manager. Select the "Scrumdinger" project and the "Swift Packages" tab, and then click the "+" button:

    

Paste in `https://github.com/realm/realm-cocoa` as the package repository URL:

    

Add the `RealmSwift` package to the `Scrumdinger` target:

    

We can then start using the Realm SDK with `import RealmSwift`.

### Update Model Classes to be Realm Objects

To store an object in Realm, its class must inherit from Realm's `Object` class. If the class contains sub-classes, those classes must conform to Realm's `EmbeddedObject` protocol.

#### Color

As with the original app's flat file, Realm can't natively persist the SwiftUI `Color` class, and so colors need to be stored as components. To that end, we need a `Components` class. It conforms to `EmbeddedObject` so that it can be embedded in a higher-level Realm `Object` class.  Fields are flagged with the `@Persisted` annotation to indicate that they should be persisted in Realm:

``` swift
import RealmSwift

class Components: EmbeddedObject {
   @Persisted var red: Double = 0
   @Persisted var green: Double = 0
   @Persisted var blue: Double = 0
   @Persisted var alpha: Double = 0

   convenience init(red: Double, green: Double, blue: Double, alpha: Double) {
      self.init()
      self.red = red
      self.green = green
      self.blue = blue
      self.alpha = alpha
   }
}
```

#### DailyScrum

`DailyScrum` is converted from a `struct` to an `Object` `class` so that it can be persisted in Realm. By conforming to `ObjectKeyIdentifiable`, lists of `DailyScrum` objects can be used within SwiftUI `ForEach` views, with Realm managing the `id` identifier for each instance.

We use the Realm `List` class to store arrays.

``` swift
import RealmSwift

class DailyScrum: Object, ObjectKeyIdentifiable {
   @Persisted var title = ""
   @Persisted var attendeeList = RealmSwift.List()
   @Persisted var lengthInMinutes = 0
   @Persisted var colorComponents: Components?
   @Persisted var historyList = RealmSwift.List()

   var color: Color { Color(colorComponents ?? Components()) }
   var attendees: String] { Array(attendeeList) }
   var history: [History] { Array(historyList) }

   convenience init(title: String, attendees: [String], lengthInMinutes: Int, color: Color, history: [History] = []) {
      self.init()
      self.title = title
      attendeeList.append(objectsIn: attendees)
      self.lengthInMinutes = lengthInMinutes
      self.colorComponents = color.components
      for entry in history {
            self.historyList.insert(entry, at: 0)
      }
   }
}

extension DailyScrum {
   struct Data {
      var title: String = ""
      var attendees: [String] = []
      var lengthInMinutes: Double = 5.0
      var color: Color = .random
   }

   var data: Data {
      return Data(title: title, attendees: attendees, lengthInMinutes: Double(lengthInMinutes), color: color)
   }

   func update(from data: Data) {
      title = data.title
      for attendee in data.attendees {
            if !attendees.contains(attendee) {
               self.attendeeList.append(attendee)
            }
      }
      lengthInMinutes = Int(data.lengthInMinutes)
      colorComponents = data.color.components
   }
}
```

#### History

The `History` struct is replaced with a Realm `Object` class:

``` swift
import RealmSwift

class History: EmbeddedObject, ObjectKeyIdentifiable {
   @Persisted var date: Date?
   @Persisted var attendeeList = List()
   @Persisted var lengthInMinutes: Int = 0
   @Persisted var transcript: String?
   var attendees: [String] { Array(attendeeList) }

   convenience init(date: Date = Date(), attendees: [String], lengthInMinutes: Int, transcript: String? = nil) {
      self.init()
      self.date = date
      attendeeList.append(objectsIn: attendees)
      self.lengthInMinutes = lengthInMinutes
      self.transcript = transcript
   }
}
```

#### ScrumData

The `ScrumData` `ObservableObject` class was used to manage the copying of scrum data between the in-memory copy and a local iOS file (including serialization and deserialization). This is now handled automatically by Realm, and so this class can be deleted.

Nothing feels better than deleting boiler-plate code!

### Top-Level SwiftUI App

Once the data is being stored in Realm, there's no need for lifecycle code to load data when the app starts or save it when it's minimized, and so `ScrumdingerApp` becomes a simple wrapper for the top-level view (`ScrumsView`):

``` swift
import SwiftUI

@main
struct ScrumdingerApp: App {
   var body: some Scene {
      WindowGroup {
            NavigationView {
               ScrumsView()
            }
      }
   }
}
```

### SwiftUI Views

#### ScrumsView

The move from a file to Realm simplifies the top-level view.

``` swift
import RealmSwift

struct ScrumsView: View {
   @ObservedResults(DailyScrum.self) var scrums
   @State private var isPresented = false
   @State private var newScrumData = DailyScrum.Data()
   @State private var currentScrum = DailyScrum()

   var body: some View {
      List {
            if let scrums = scrums {
               ForEach(scrums) { scrum in
                  NavigationLink(destination: DetailView(scrum: scrum)) {
                        CardView(scrum: scrum)
                  }
                  .listRowBackground(scrum.color)
               }
            }
      }
      .navigationTitle("Daily Scrums")
      .navigationBarItems(trailing: Button(action: {
            isPresented = true
      }) {
            Image(systemName: "plus")
      })
      .sheet(isPresented: $isPresented) {
            NavigationView {
               EditView(scrumData: $newScrumData)
                  .navigationBarItems(leading: Button("Dismiss") {
                        isPresented = false
                  }, trailing: Button("Add") {
                        let newScrum = DailyScrum(
                           title: newScrumData.title,
                           attendees: newScrumData.attendees,
                           lengthInMinutes: Int(newScrumData.lengthInMinutes),
                           color: newScrumData.color)
                        $scrums.append(newScrum)
                        isPresented = false
                  })
            }
      }
   }
}
```

The `DailyScrum` objects are automatically loaded from the default Realm using the `@ObservedResults` annotation.

New scrums can be added to Realm by appending them to the `scrums` result set with `$scrums.append(newScrum)`. Note that there's no need to open a Realm transaction explicitly. That's now handled under the covers by the Realm SDK.

### DetailView

The main change to `DetailView` is that any edits to a scrum are persisted immediately. At the time of writing ([Realm-Cocoa 10.7.2), the view must open a transaction to store the change:

``` swift
do {
   try Realm().write() {
      guard let thawedScrum = scrum.thaw() else {
            print("Unable to thaw scrum")
            return
      }
      thawedScrum.update(from: data)
   }
} catch {
   print("Failed to save scrum: \(error.localizedDescription)")
}
```

### MeetingView

As with `DetailView`, `MeetingView` is enhanced so that meeting notes are added as soon as they've been created (rather than being stored in volatile RAM until the app is minimized):

``` swift
do {
   try Realm().write() {
      guard let thawedScrum = scrum.thaw() else {
            print("Unable to thaw scrum")
            return
      }
      thawedScrum.historyList.insert(newHistory, at: 0)
   }
} catch {
   print("Failed to add meeting to scrum: \(error.localizedDescription)")
}
```

### CardView (+ Other Views)

There are no changes needed to the view that's responsible for displaying a summary for a scrum. The changes we made to the `DailyScrum` model in order to store it in Realm don't impact how it's used within the app.

    
    Cardview

Similarly, there are no significant changes needed to `EditView`, `HistoryView`, `MeetingTimerView`, `MeetingHeaderView`, or `MeetingFooterView`.

## Summary

I hope that this post has shown that moving an iOS app to Realm is a straightforward process. The Realm SDK abstracts away the complexity of serialization and persisting data to disk. This is especially true when developing with SwiftUI.

Now that Scrumdinger uses Realm, very little extra work is needed to add new features based on filtering, synchronizing, and sharing data. Let me know in the community forum if you try adding any of that functionality.

## Resources

-   Apple's tutorial
-   Pre-Realm Scrumdinger code
-   Realm Scrumdinger code
-   Diff - all changes required to migrate Scrumdinger to Realm

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