Let’s talk about Core Data
I know.. It’s weird
Before Core Data
The time is 08:17 pm and you’re late for work. The night before was really crazy and you forgot to charge your phone overnight and you barely got up from bed. You get on the subway all stressed out and write this long text to your boss explaining and begging her not to fire you. And just before you press the big green “send” button your phone dies. You start to panic even more, but discover a power-bank in your backpack so you immediately begin to charge your phone. When it boots up and you enter the message app your long and precious message is gone. Instead of writing a new message you run to the office and meet the boss who has already begun packing your stuff. And she says, “A simple text would prevent this”.
I know, I know, that description is really long and weird, but it was like that before persistence data. Don’t get worried about that highlighted word over there, I have an explanation:
Persistence: “Saving data to a place where it can be re-accessed and retrieved upon restart of the device or app which is necessary for any app that wants to store data long term and keep it available to the user”
I know what you’re thinking. “Wasn’t this article about Core Data and not about persistence?”
And you’re right, but Core Data is just another common option of the data persistence options. The four most common are Default System, Property Lists, SQLite and Core Data.
It just happens that Core Data is dominating every other option right now. But why?
Why Core Data?
Core Data is basically a framework like UIKit, but a bit more complex. It is used to manage models or data. There are a couple of built-in features that will make a difference:
- 1. Filtering
- 2. Save on the disk
- 3. Undo and redo to data
- 4. Partial loading unlike UserDefaults
- 5. Change tracking of data
Okey, that sounds great. But what is Core Data?
What is Core Data?
Core data is Apples almost proprietary system for persistent data storage. It’s basically a framework that you can find on Github, but a bit more complex.
Core Data is also not a database. If you’re familiar with MySQL, you might know that relational databases stores data in the form of row, table, and column. So, don’t mix up Core Data with a database. Though SQLite database is the default persistent store for Core Data on iPhone, Core Data is not a relational database.
When only using Core Data you can map the objects in your apps to the tables records in the database, without knowing any SQL.
How should I learn Core Data?
As our own Aristotle once said, “For the things we have to learn before we can do them, we learn by doing them.”
It’s all about implementing your knowledge in real-life projects and just do it!
If you want to learn Core Data I would recommend building a ToDo-List app. It will teach you the foundations about Core Data, such as Attributes and Relationships. Afterward you can start taking on more complex projects, like a dream lister app (Github source code here) that will teach you about Relationships in Core Data.
Udacity is one of many websites that has great tutorials about Core Data.
I haven’t deep dived into Core Data (yet!), but I hope this could be a starting point for all you developers out there. I also want to point out that this is my first Medium post, so go soft on me!
I have many goals this year. The most important one is to become a better developer in general, but also reach 500 followers on Instagram (link below). But after writing this article I want to add one more goal to my list and the goal is about me writing a Medium article per week. So, every Monday I will pop up in your feed if you start following me.
Persisting data across application launches is a requirement that most iOS applications have, from storing user…code.tutsplus.com