TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Updating SwiftUI when Core Data changes with computed property

Forums > SwiftUI

I ran into an issue where I created a computed property in one of my SwiftUI views that does a couple of Core Data queries to get a result. Unfortunately, if the managed object context changes such that that property getter needs to be re-run, it doesn’t happen automatically.

How can I tell SwiftUI to call the getter again when there’s a change in Core Data? Is the best way to listen for NSManagedObjectContextDidSaveNotification in the view and execute the query then, and just stick that in an @State property in the view?

Ohhh, it should probably be a @Published property in my model, and do the listening there, huh?

2      

there is no need to use @Published , from what i know @FetchRequest can get you latest updated data with out any additional code ..

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.