Realm with SwiftUI, macOS and Table


I am trying out Realm with swiftUI and macOS for the first time and I have encountered an issue. I belive it is a simple solution.

I would just like to list items in a table. (not list). Then I would like to select a row and hit an edit button that pops up a new window where I can edit all the details. Then click a save button and close the window. The data in table must be updated with new info.

If anyone could give me some hints on how to do this with swiftUI and macOS I would be very happy.


I haven’t worked with tables outside of Apple’s macOS tutorial, but it looks very similar to working with Lists, so I think the @ObservedResults or @ObservedRealmObject property wrappers would work great for this case.

Apple’s SwiftUI on the Mac: Build the Fundamentals tutorial has you building a garden tracking app. Here’s the sample project code: Building a Great Mac App with SwiftUI. In that code, Apple’s app uses an @EnvironmentObject var store: Store, from which you get plants to populate the table.

Depending on the structure of your app, you could use either @ObservedResults or @ObservedRealmObject in place of this environment object to store your garden details and populate your table. ObservedResults is an observed list of query results that invalidates/updates the view when objects in the results set are updated. ObservedRealmObject observes a specific object and automatically invalidates/updates the view when the object changes. Our SwiftUI Guide has some more details that might help you decide how best to structure this. Or check out our Realm + SwiftUI Quickstart to see these property wrappers in action.

For more complex cases, I haven’t watched it yet, but Stewart Lynch recently released a YouTube video tutorial about using Realm + SwiftUI with Notification Tokens instead of the @ObservedResults property wrapper. If you’ve got a more complex case where our @Observed property wrappers don’t work for you, this tutorial might help.