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)
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments