Quick Comparison of Two Powerful Frameworks for SQLite when creating tables: Active Record and Core Data.

Alex Mendes
4 min readSep 24, 2019

--

SQLite is the most used database engine in the world. SQLite is built into all mobile phones and most computers and comes bundled inside countless other applications that people use every day.

SQLite database files are commonly used as containers to transfer rich content between systems and as a long-term archival format for data. And Frameworks for that matter, make it possible for the application to talk to the database in a more organized and efficient way to coding.

Active Record:

Active Record is the M in the MVC — the model — which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping(ORM) system.

Core Data:

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

Now that we understand the technologies, let’s use each one of the frameworks to create a table called “Students”.

Starting with Active Record:

1. Make sure the environment.rb is set up correctly.

This is the part where you are going to establish a connection between Active Record and SQLite. If you are using web frameworks like Rails to create your application, you don’t have to worry about creating that bridge, but it’s always good to understand where the relationship comes from.

Establish a connection between Active Record and SQLite

2. Creating Migration.

Once the connection was established, we need to create our table. But first, we need to create the migration file that allows us to insert the attributes in our table typing the following bash command in our app root directory.

$ rake db:create_migration NAME=create_students

3. Fill out table instructions

With the above command, a migration file gets created. Allowing us to later fill it out with instructions to create its table’s properties.

Example of a migration file.

4. And last but not least, execute the migration.

$ rake db:migrate

Now, let’s analyze the same process but using Core Data instead.

Creating tables with Core Data:

In order to demonstrate the basics of the Core Data, let’s create a single view iOS app and selected the Core Data module.

Once we create the project with Core Data support, there are two notable changes in the Xcode template:

  • The new fileCoreDataDemo.xcdatamodeld
  • And the AppDelegate.swift file with Core Data Stack code
The constant container is our connection between Core Data and the database. Similar to what we did inside environment.rb.
The function that allows persisting data inside the container

The new file CoreData_example.xcdatamodeld acts as the model layer for the data that we want to save. We can easily ad the entity, attributes and relationships from the UI as like any other database.

Inside the .xcdatamodeld file, you can name the table by clicking on “Add Entity” (+) button (as a convention, tables should always be plural) and inside the attributes tab, name it and specify the attribute’s type of data and that’s it! You just created your table in Swift using Core Data.

Conclusion:

People use frameworks primarily to lessen the mental burden of developing on the web. By giving people a learnable process, the framework should allow beginners to make progress and experts to progress quickly. The process should be the primary focus; the framework should exist only to support it.

And another takeaway from all of this is that as programmers, we give too much credit when comes to learning the syntax, but understanding the process and its concept is more important than remembering snippets of codes. Especially when it comes to transferring our knowledge into learning new technologies, internalizing such ideas will make the learning process a more smooth and definitely a lot easier.

--

--

Alex Mendes

Web and Mobile Developer. Software Engineering graduated from Flatiron School, Sports Lover, From Planet Earth.