how to restrict entry of duplicate object in core data

Sarthak

How can i restrict duplicate entry in core data

//MARK:- save in core data
let appDel1:AppDelegate = UIApplication.shared.delegate as! AppDelegate
let context:NSManagedObjectContext = appDel1.persistentContainer.viewContext
let userObj:NSManagedObject = NSEntityDescription.insertNewObject(forEntityName: "RPAData", into: context)
                
userObj.setValue(rpaType, forKey: "rpatype")
userObj.setValue(rpaReg, forKey: "rparegistration")
userObj.setValue(identificationNumber, forKey: "identificationnumber")
userObj.setValue(rpaTypeId, forKey: "rpatypeid")
                
                do{
                    try context.save()
                    print("rpaReg , identificationNumber , rpaTypeId and rpaType Saved in core Data...")
                } catch {
                    print("Error in save - ", error)
                }

If the combination of all the 4 entries(rpaReg , identificationNumber , rpaTypeId and rpaType) are same than these data will not save in core data and an alert will be shown.

this is how i fetch data from core data

var dataArray:[NSManagedObject] = []

let appDel1:AppDelegate = UIApplication.shared.delegate as! AppDelegate
let context:NSManagedObjectContext = appDel1.persistentContainer.viewContext
let fetchRequest:NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "RPAData")
            do
            {
                dataArray = try context.fetch(fetchRequest) as! [NSManagedObject]
            } catch {
                print("error =>",error)
            }
erictruong

Add some constraints in your entity enter image description here

Then use this to show error when saving failure (default value)

context.mergePolicy = NSErrorMergePolicy

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MySQL - How to remove duplicate data entry?

How to avoid duplicate data entry in a table in Room Db?

How to restrict Firebase data modification?

How to update existing object in core data ? [Swift]

How update object with relation data in EntityFramework Core

Tkinter: restrict entry data to float

moving table data error ‘#1062 - Duplicate entry‘

Is there anyway to restrict an object to a particular data type in Python?

MySql.Data.MySqlClient.MySqlException: Duplicate entry

How to prevent duplicate entry in sqlite

Duplicate data in object setter

Duplicate key entry when updating object

How to restrict object types based on a value in the object?

How to save to Core Data RKMappingResult object in RestKit?

Swift: How to check a core data NSSet for an object

How to create a relationship with existing object in core data

How do I avoid duplicate object entry in Mongodb?

How to fetch data without a duplicate entry by using limit and order by

how to override data in core data ? how to solve duplicate data in Core Data?

how to prevent duplicate entry in mysql while data are entered from array?

How to restrict DUPLICATE EMAILS insert in MongoDB?

How to solve a Duplicate zip entry

How to counter duplicate entry in laravel

fetch data from mysql and replace duplicate entry

Adding duplicate data into one entry

Validate Duplicate Data Entry in Array - JavaScript

Foundry Workshop - Prevent duplicate data entry

how to get multiple duplicate data in array object

How to warn for duplicate entry vba