Another way to persist data in Android is to use Realm. In this video, Annyce talks about what Realm is and how you can use it to store your application’s data.
- [Instructor] Another way to persist data in Android, is to use Realm. Let's talk briefly about what Realm is, and how you can use it to store your application's data. Realm is a NoSQL mobile database. It does not rely on SQLite in any way. In fact, it exposes your stored data as objects. Therefore, when we talk about columns in Realm, they aren't really columns, they're properties of objects.
In this course, I'll be using the words columns, and properties interchangeably. What's more, Realm uses a zero-copy design. This means that as much as possible, you are interacting directly with values in the database. This allows Realm to be much faster than a typical ORM, and it's often faster than Raw SQLite. Finally, Realm allows you to easily model and query relationships between objects.
Objects in Realm, extend the Realm Object class. This lets Realm know that you want this particular object to be included in your database's schema. Then, the fields that you declare in your object are used as the columns more or less. Therefore, let's assume we have a field name of Typestring. Then we would declare it as follows. And you can visualize this data as being stored in a table with a column named Name, just like we see here.
If we compare this to how we'd achieve something similar, with Vanilla SQLite, recall we first had to create a contract class where we listed out all of our field names. Next, we had to write a SQL create statement that would generate the table for us. And finally, we had to execute this statement in our database helper class.
Now, we can simply define the fields as we normally would with a java object, and Realm takes care of the rest. Let's take a look at the website for the Java mobile database. You can see that Realm is installed as a gradle plug-in. This is done in a two-step process. First, you add the class path to your main build.gradle file, and then you apply the plug-in to your application's build.gradle file.
Once you refresh your dependencies, you will have access to the needed Realm classes. Now let's get started using Realm in our application.
To begin, Annyce Davis reviews using the de facto data persistence solution available on Android: SQLite. She takes you through basic SQLite concepts—such as how to create a database wrapper and insert data into a table—as well as a few more advanced topics. Next, she covers working with Cupboard, an open-source project dedicated to simplifying your SQLite interactions. To wrap up, she dives into working with Realm, an object database solution designed with mobile devices in mind. Throughout the course, Annyce discusses establishing relationships between database tables, creating and running queries, as well as performing data migrations.
- Overview of SQLite
- Defining a database table's schema
- Exploring SQLite create table syntax
- Executing create table statements
- Inserting data with a foreign key relation
- Inspecting the database using a terminal
- Updating and deleting records
- Working with Cupboard
- Working with Realm