Basic CoreData in iOS Swift

Basically , Core Data is a graphical and persistence framework, which is used in Apple devices with operating systems macOS and iOS

Core Database Using swift

Core Data is Store the our data in our phone like simple applicatine make like only used one device & access that all data in only one device if its uninstall then all the data are remove .

Step -1

Create the core data application please Core DataUse core Data” it’s bottom of Language selection

Step -2

Core Data is well integrated in Xcode, providing a visual interface for definition entities, properties, and relationships. To open the Data Model Editor, select a .xcdatamodeld

create the Entity with entity name and attribute name with your Attribute name and datatype

Step -3

Create the Databse(For my database)

func createIteam(name:String){
let newIteam = ToDoListItem(context: context)
newIteam.name = name
newIteam.createAt = Date()
do{
try context.save()
getAllItems()
}catch{
}
}

Step -4

Featch all the databse value

func getAllItems(){
do{
model = try context.fetch(ToDoListItem.fetchRequest())
DispatchQueue.main.async {
self.tblView.reloadData()
}
}catch{
}
}

Step -5

Update all the databse value

func updateIteam(item :ToDoListItem, newName : String ){
item.name = newName
do{
try context.save()
getAllItems()
}catch{
}
}

Step -6

Delete all the databse value

func deleteIteam(item:ToDoListItem){
context.delete(item)
do{
try context.save()
getAllItems()
}catch{
}
}

If you have using Image then write the code like

import Foundation
import UIKit
import CoreData

class DatabaseHelper{
static let instance = DatabaseHelper()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

func saveImageinCoreData(at imageData : Data){

var profile = NSEntityDescription.insertNewObject(forEntityName: “Profile”, into: context) as! Profile
profile.img = imageData
do {
try context.save()
}catch let error{
print(error.localizedDescription)
}
}

func getAllImages() -> [Profile]{
var arrProfile = [Profile]()
let featchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: “Profile”)
do{
arrProfile = try context.fetch(featchRequest) as! [Profile]
}catch let error{
print(error.localizedDescription)
}
return arrProfile
}
}

Button click event

@IBAction func btnSaveImgClick(_ sender: UIButton) {

// let jpg = self.imgFirst.image?.jpegData(compressionQuality: 0.75)

if let png = self.imgFirst.image?.pngData(){

DatabaseHelper.instance.saveImageinCoreData(at: png)

}

var arr = DatabaseHelper.instance.getAllImages()

self.imgSecond.image = UIImage(data: arr[0].img!)

}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Meghana Trivedi

Meghana Trivedi is a Software Developer who helps Startups and local Entrepreneur develop their idea into product and reach the right audience.