UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Core Data and SwiftUI Life Cycle

Forums > SwiftUI

I tried to repeat what Paul did in this example. One-to-many relationships with Core Data, SwiftUI, and @FetchRequest However, I got a different result. My result vs. Paul's result

I guess the thing is that I used SwiftUI Life Cycle. How can I fix it?

UPDATE I tried to create new project, but this time based on UIKit App Delegate Life Cycle. And ... the result is the same - a new entity Country is created for each entity Candy (even if it already existed). Xcode 12.3

SOLVED

for UIKit App Delegate Life Cycle: Ensuring Core Data objects are unique using constraints

If you want Core Data to write the changes, you need to open SceneDelegate.swift and add this import:

import CoreData

Now add this line in the willConnectTo method, directly below the code that starts let context:

context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

for SwiftUI Life Cycle: SOLVED: Core Data and Lifecycle SwiftUI App

add in Persistence.swift

container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.