Opening read-only Realm before entering view

I am attempting to open a read only Realm and then passing in an object from that Realm before entering a view because I remember reading somewhere that this is what I should do.

I am using SwiftUI and have a View called HuntCard that is reading values from a Huntlet object that is part of user.hunts in the “user=user._id” partition.

When tapping on the card it needs to load the read-only Realm that contains a Hunt object.

I have attempted to do it like this:

NavigationLink(destination: HuntView(hunt: self.hunt), isActive: $realmReady) {
  Button(action: {
    getHuntRealm()
}
  }) {
      Text(huntlet.title)
    }

Then, the important code:

getHuntRealm() {
  let realmConfig = user?.configuration(partitionValue: "hunt=\(huntlet.code)")
  do {
    self.huntRealm = try! Realm(configuration: realmConfig!)
  }
  if self.huntRealm != nil {
    let hunts = self.huntRealm.objects(Hunt.self)
    if hunts.count > 0 {
      self.hunt = hunts[0]
      self.realmReady = true
    }
  }
}

The code never gets past hunts.count > 0 even though there is a hunt in this partition.

Here is the Huntlet object in the User Realm:
Screen Shot 2021-02-17 at 7.23.26 AM

Here is the corresponding Hunt object in the Hunt Realm:

I haven’t seen any clear documentation on how this should be done, opening the Realm and loading the object and then passing that object to the view (to HuntView in this case).

Is this in the documentation somewhere? The opening/closing of these seem to be done in quite a few different ways amongst examples and I can’t for the life of me decipher how it works and implement it in my own app.

Thanks for any guidance here.

–Kurt

Hi Kurt, using Realm-Cocoa 10.6, I’d start with something like this (apologies if I have my Hunts and Huntlets the wrong way around):

import SwiftUI
import RealmSwift

struct HuntletView: View {
    @State var showingHunt = false
    let huntlet: Huntlet
    
    var body: some View {
        NavigationView {
            Button(action: { self.showingHunt.toggle() }) {
                Text(huntlet.title)
              }
            NavigationLink(
                destination: HuntView().environment(\.realmConfiguration, app.currentUser!.configuration(partitionValue: "hunt=\(huntlet.code)")),
                isActive: $showingHunt) { EmptyView() }
        }
    }
}

struct HuntView: View {
    @ObservedResults(Hunt.self) var hunts
    
    var body: some View {
        if let hunt = hunts.first {
            Text(hunt.title)
        }
    }
}

Thanks Andrew!

This works. I caught most of this during the webinar yesterday and really appreciate it.

Side note for people that might see this and run into this issue, the realmConfiguration that is passed in .environment() is passed down to other sub-views. I accidentally passed it down again. It worked locally, but it breaks sync and stopped reaching Atlas. When I removed the second .environment() and just trusted that it would be passed down, everything works as it should.