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:
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