Introduction to core data in iOS Swift 5
Today we are going to talk about Core data in iOS development.
Core data is used to store data locally in your project. Core data is a built-in framework of Xcode to store data locally in the application.
In this tutorial we will be doing complete Core-Data intial setup and save some values to Core-data.
Let's get straight to the development.
Open Xcode and create a new project with Core data checkmark enabled.
Click next and create your new project. Now in Xcode files you will see .xcdatamodeld file. Open this file and you will see an empty screen with some options.
Click on add Entity to create a new entity for your local data.
By clicking on Add Entity a new entity will be created. Rename it to whatever your requirement is. Eg. I am renaming it to Employee. Now you need to click on attributes and create some attributes for the entity.
Note- **Entity name should start with Capital letter and attributes should start from small letter.**
I have created four attributes for the Employee entity: name, address, phone, email.
Now, after creating attributes select Entity which you have created, click on show data model inspector and select Codegen to Manual/None.
After this, we need to click on entity again, and then Editor > Create NSManageObject Subclass. Click next and add these subclasses to the project.
Now, two NSManageObject classes will be added to your project. These class will contain instance of entity and attributes that you had created earlier in the .xcdatamodeld file.
This will complete the initial setup for the core data.
Now, for saving data in Coredata. I am creating a simple form UI in storyboard with textfields and a save button. Now create outlets of textfield and button action in the viewcontroller.
So, the basic setup has been completed.
Now I am going to create a new Swift file for managing all the requests to and from the database (Core Data) with name DatabaseManager.
Why do we need to create a DatabaseManager class? It will exclude all the query logics from the Main ViewController and will be good in readability.
For now we have added Save function only so everyone have better understanding.
Now go to your ViewController class and add this in Save Button action function
Now run the application, fill in the textfields and click on save button. Your data has been saved to CoreData.
To cross check whether your data is saved or not. Go to AppDelegate and in didFinishLaunching with options add this line:
print ("Document directory: ", FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last ?? "Not Found")
This line will print the path of core data where data has been saved.
Now run application again, and see printed path in console eg. file:///Users/saurabh/Library/Developer/CoreSimulator/Devices/D120B06B-96ED-4378-B99C-CB66F74A65C9/data/Containers/Data/Application/C659431C-5934-482D-AF71-5F59F90B5B46/Documents/.
From the printed path ignore file:// and copy the rest Path. Right click on Finder and Go to folder >> paste path. Go back and click on Library >> Application Support >> projectname.sqlite
You can use either DBBrowser or any other tool to view db file and saved data.
I am using DBBrowser in my case and data is shown like below:
Nice and do remaining CRUD operations...
ReplyDeleteThanks, will add another blog on them soon.
ReplyDelete