Realm with SwiftUI Tutorial: Getting Started | raywenderlich.com

Thanks for letting me know.

1 Like

Hi. Thank you for this tutorial. It’s been incredibly helpful. I’m just stuck on trying to apply the knowledge to my app.

Specifically, how can I add realm to the environment with the new main App struct instead of inside the old SceneDelegate?

Any help is greatly appreciated.

Hi @jakesteramma31
Thanks for reading the tutorial. I’m really glad you liked it.

In regards to your question. You can simply use environmentObject(_:) to pass a Realm instance. However, you should be careful though, realm dos a couple of transaction whenever you open and close a realm. That is, when you instantiate a Realm object and release it from memory. If you’re not careful, you might run into some thread racing problems.

Also, Realm has updated recently with property wrappers and a default realm object on SwiftUI’s environment, so you don’t have to worry about that. You can change its configuration by setting the environment property \.realmConfiguration to a Realm.Configuration that you want.

Hi,

First of all thanks for the tutorial. It was really helpful in my app. However when I try to call the " IngredientListView(ingredients: store.ingredients, boughtIngredients: store.boughtIngredients)" section of the code through another view using a Navigation Link, I get this error.

No ObservableObject of type IngredientStore found. A View.environmentObject(_:slight_smile: for IngredientStore may be missing as an ancestor of this view.

However I have already added “.environmentObject(store)” to the end of the code and I am still getting this error. :confused: Any idea as to what is going on here?

Hello @saltconstriant,

Can you post a sample of the code that’s happening this? This usually happens because even though you’re adding the store with environmentObject(_:) , the object is not in the hierarchy of the next view. Usually, NavigationView already passes the environment object to next views. If this is not happening, environmentObject(_:) might not be in the right hierarchy.

Hi,

Thanks for the reply. Here is line of code that causes the app to crash.

NavigationView {
IngredientListView(ingredients: store.ingredients, boughtIngredients: store.boughtIngredients).environmentObject(store)
}

Thanks!

Hi @saltconstriant . Try moving your environment object to the most outer view. I suspect this is happening because when you add the environment object to a NavigationLink, the environment does not match the environment of the NavigationView. So when the view is pushed, the environment of the Navigation views does not have the necessary object.

Hi, that still did not work, I think it has something to do with the other parts of my app. I will continue taking a look, but thanks for your input.

Will this article be updated for the current Realm Swift that has so many enhancements for SwiftUI? The title is “Realm with SwiftUI Tutorial: Getting Started”. It was helpful several years ago, but otherwise it’s not very relevant today. At least provide an addendum update that shows what to change, like @Persisted, ObjectId, etc. I tried to make such changes, but failed to get it working.

Hi @warren ,

I’m glad you’re interested in this article. We have an update on the pipeline and should have it published in the near future. :]

That’s great to hear. I was excited when I saw this tutorial, but then it was out of date with current capabilities for Realm with SwiftUI.