Core data in iOS

Tram Ho

Hello everyone, today’s article I will write about Core data in iOS, I write the most basic things that those who just started learning iOS also understand the basics. We think of using core data when we want to store data locally, without having to call API. I start on the demo too.

First, create a new project named CoreDataDemo, core data is built into iOS, so when you create a new project with the option to use Core data or not, I choose yes:

After creating the project, XCode makes available to us the CoreDataDemo.xcdatamodelId file, where we can create Entities and add Attributes to them. At the bottom left we click Add Entity and give it a name. Then we click on the newly created Entity, and choose Add Atribute. And in AppDelegate there are 2 components available, persistentContainer variable and saveContext () function to support saving and getting information from core data, each function has a comment explaining details.

Now I open the CoreDataDemo.xcdatamodelId file and create Entity named User, and have 2 attributes named name and age as follows:

When using core data, we can see explicit Entity in the form of a table, and the relationship between Entities as well:

Now I create a tableview to display the list of users we store in core data, and an Add button to add the user to the core data:

Create an array of data to show to tableview:

NSManagedObject represents an object stored in core data, you need to use it to create, edit, save and delete from your core data. Next, I add the data source functions of the table view as follows:

Next, I write the code to add the user to the core data as follows:

I explain:

  1. Before you can save or retrieve anything from CoreData, you need to get the NSManagedObjectContext, which XCode was already in AppDelegate when I created the app.
  2. Create a new managed object and insert it into the managed object context. NSManagedObject represents the entity, an NSEntityDescription is the part associated with the entity definition from the Data Model to an instance of NSManagedObject at runtime.
  3. Set attributes for the data model.
  4. Commit your changes to the user and save to memory by calling the save method. Note that save with throw error must use try catch. Finally, add an object to the users array to display it on the tableview.

Now, when running the application, you can add data user to core data:

When you run the application again, need to show the list of users saved just now to tabview, we will write the code to get the data out as follows:

I explain:

  1. Before doing anything, you need to call the NSManagedObjectContext object as mentioned above.
  2. NSFetchRequest is a class that handles fetching from CoreData. Fetch requests are very useful and flexible. You can use the fetch request to fetch the collection of objects (like you would get all the employees in the company …) Not only get all of them, but we can rely on NSEntityDescription to filter many different conditions for return results.
  3. You process your fetch request with fetch () to return the array of objects according to the request’s condition, reload the tableview.

Rerun the app, you will see the data shown as follows:

My post here is over, thank you for reading my posts ^^. Document link: https://www.raywenderlich.com/7569-getting-started-with-core-data-tutorial

Share the news now

Source : Viblo