I’m working on something that seems pretty straight forward but it seems that SwiftUI actually is making it more difficult with live objects.
My app is a simple scavenger hunt app. Create hunts. Add tasks to the hunts.
In my model I have Hunt
objects and Task
objects.
The Hunt has a list of Tasks:
let tasks = RealmSwift.List<Task>()
And the Task has an inverse relationship reference:
let hunt = LinkingObjects(fromType: Hunt.self, property: "tasks")
I’m passing the property down through the components, first just the hunt, then when it gets to the TaskListView, I’m passing in hunt: hunt
and tasks: hunt.tasks
When I add a new task, if I leave and return to the view, it will update, but it won’t update without leaving the screen and returning.
I assume this is a notification issue, but the quickstart doesn’t mention notifications with SwiftUI here: https://docs.mongodb.com/realm/ios/swiftui
The hunts are loaded as hunts = RealmManager.shared.userRealm.objects(Hunt.self)
and then passed down, but they are frozen like this:
ForEach(hunts.freeze()) { hunt in if let huntsRealm = hunts.realm { HuntCardView(hunt: (huntsRealm.resolve(ThreadSafeReference(to: hunt)))!) } }
How would I add a listener/notification so that this will refresh the view when a new task is added?
Side note: I have been using Realm for years in React Native, but just now moving to MongoDB Realm and SwiftUI. Just using .addListener()
was very straight forward in RN. Hoping there is a similar method in SwiftUI.