Getting Started Save, Fetch, Update and Delete Core Data in Swift 5.0
Core Data is NOT an SQLite database. it is a graphical and persistence framework.CURD in swift (Create, Update, Retrieve, Delete)
//MARK: - Save User Info Data
func addData(user_name: String, user_email: String, user_dob: String, imageData: NSData) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "UserInfo", in: managedContext)!
let UserData = NSManagedObject(entity: entity, insertInto: managedContext)
UserData.setValue(user_name, forKeyPath: "user_name")
UserData.setValue(user_email, forKeyPath: "user_email")
UserData.setValue(user_dob, forKeyPath: "user_dob")
UserData.setValue(imageData, forKeyPath: "image")
do {
try managedContext.save()
getUserInfo.append(UserData)
print("save data.")
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
//MARK: - Fetch User Info Data
func fetchData(){
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "UserInfo")
do{
let results = try context.fetch(fetchRequest)
let dateCreated = results as! [NSManagedObject]
for _datecreated in dateCreated {
userListData.append(_datecreated)
tableView.reloadData()
}
}catch let err as NSError {
print(err.debugDescription)
}
}
//MARK: - Delete User Info Data
func deleteInfo(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let requestDel = NSFetchRequest<NSFetchRequestResult>(entityName: "UserInfo")
requestDel.returnsObjectsAsFaults = false
let predicate = NSPredicate(format: "(user_name = %@)", deleteValue)
requestDel.predicate = predicate
do {
let arrUsrObj = try context.fetch(requestDel)
for usrObj in arrUsrObj as! [NSManagedObject] { // Fetching Object
context.delete(usrObj) // Deleting Object
}
} catch {
print("Failed")
}
do {
try context.save()
} catch {
print("Failed saving")
}
}
//MARK: - Update User Info Data
func UpdateInfo(){
let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "UserInfo", in: managedContext)
let request = NSFetchRequest<NSFetchRequestResult>()
request.entity = entity
let predicate = NSPredicate(format: "(user_name = %@)", userNameStr)
request.predicate = predicate
do {
let results =
try managedContext.fetch(request)
let objectUpdate = results[0] as! NSManagedObject
objectUpdate.setValue(userName_textField.text!, forKey: "user_name")
objectUpdate.setValue(userEmail_textField.text!, forKey: "user_email")
objectUpdate.setValue(userDOB_textField.text!, forKey: "user_dob")
do {
try managedContext.save()
userName_textField.text = ""
userEmail_textField.text = ""
userDOB_textField.text = ""
print("Update")
}catch _ as NSError {
print("Update")
}
}
catch _ as NSError {
print("Update")
}
}
Comments
Post a Comment