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?

Tkinter: restrict entry data to float

How do I avoid duplicate object entry in Mongodb?

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

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

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

Adding duplicate data into one entry

how to get multiple duplicate data in array object

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

How to counter duplicate entry in laravel

How to prevent duplicate entry in sqlite

How to solve a Duplicate zip entry

How to warn for duplicate entry vba

How to restrict DUPLICATE EMAILS insert in MongoDB?

Duplicate key entry when updating object

Duplicate data in object setter

MySql.Data.MySqlClient.MySqlException: Duplicate entry

fetch data from mysql and replace duplicate entry

moving table data error ‘#1062 - Duplicate entry‘

Validate Duplicate Data Entry in Array - JavaScript

Foundry Workshop - Prevent duplicate data entry

How to create a relationship with existing object in core data

How to save to Core Data RKMappingResult object in RestKit?

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

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

How update object with relation data in EntityFramework Core

How to restrict Firebase data modification?

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

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