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

CoreDataProject - Writing duplicate records

Forums > 100 Days of SwiftUI

Just completed the section on Dynamic Filtering, which works fine. The 'Add' button keeps adding the same records. Mistakenly, I was under the impression that the SceneDelegate code:

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

avoids writing duplicate data. Obvioulsy, it only merges what is in the cache, before saving. Anyone have some ready made code that validates additions against the DB first? Regards.

3      

I tried to find the lesson in 100 Days of SwiftUI and wasn't able to find it. Could you please give me a link or the code snippet which adds the object? Because I doubt it's the same object but it's everytime a new instance of the object with the same property values.

Have you seen these articles here: https://www.hackingwithswift.com/books/ios-swiftui/ensuring-core-data-objects-are-unique-using-constraints https://www.hackingwithswift.com/read/38/6/how-to-make-a-core-data-attribute-unique-using-constraints

3      

Thank you Hatsushira.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.