How to use basic Core Data with your Swift Project

Posted By : Aditya Kumar Sharma | 16-Mar-2016

Apple provides a built in data management library known as Core Data. It can reduce the memory overhead of your app, increase responsiveness, and save you from writing a lot of boilerplate code. In this blog we are going to learn how to use Core Data in our application.

Creating a Core Data Project

  •  Open Xcode and create a new Project, choose the template Single View Application as shown below.

?

  • In next screen, enter YourProjectName as a name of the project, select iPhone in Devices family and then select the options Use Storyboards, Use Core Data, Use Automatic Reference Counting. Press next and create.

?

  • The first step is to open the Data Model named YourProjectName.xcdatamodeld and define the object model. Here we’ll define a Device entity which will be used to store the data to database. To create an entity, click the + button in the bottom-left of the editor view and name the entity as Entity.

?

  • As we created a new Entity, now we need to add attributes to it. Click + button in the attribute section to do that. We can add as many attributes as we require and set there type.
  • Now we will create the subclass of the NSManagedObject class. For creating, select the Editor -> Create NSManagedObject Subclass…  menu option to display the dialog:

?

  • Make sure that the YOURPROJECTNAME data model is been selected before clicking Next..

  • Check that Entity is selected before moving on the Next. 

?

  • A file will be created named Entity.swift to the project containing the NSManagedObject subclass for the entity which look like this:

  • The next thing is to create Storyboard which defines the views of the app. 

Diving into Core Data

With the user interface, now we will handle the Core Data. Apparently, there are a couple of areas we have to implement:

  • Save information in the View Controller
  • Fetch information from persistent store (i.e. SQLite database) and populate the data where ever required.

?Accessing the Managed Object Context

 We need to implement following code to store and retrieve data using Core Data. A reference  to the application delegate’s managed objet context is required. Within the ViewController.swift file, import the CoreData Framework and add a variable to store this reference as follows:

Saving Data to the Persistent Store using Core Data

When we save the data saveData method is called. It is within this method, so we need to  implement the code to create and store managed objects containing the data entered. Select the ViewController.swift file and implement the code as follows:

Retrieving Data from the Persistent Store using Core Data

  • Now for fetching the saved data we have showData method. This method will need to identify the entity description for the Entity and then create a predicate to ensure that only objects with the name specified are retrieved from the store.

  • Now we are ready to build and run the application.

 

Thanks

About Author

Author Image
Aditya Kumar Sharma

Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.

Request for Proposal

Name is required

Comment is required

Sending message..