Getting Started with Paper Database in Android
A database is an organized collection of data that is stored in computer systems. <!--more--> In Android, there are several databases such as SQLite, Room, SQLDlight, Datastore, SharedPreferences, and Realm database.
Although most people think that all databases are composed of fields and records, databases held on Paper contain fields and have no records. An example is the phone book which has no records for each person. Instead, it has the field as the main element of the database.
Table of contents
- Prerequisites
- Goals
- What is a Paper database?
- Advantages of Paper database
- Disadvantages of Paper database
- When to use Paper database
- Getting started
- Creating a new project
- Adding dependencies
- Creating the user interface
- Initializing Paper in an application class
- Creating the model class
- Inserting data to Paper database
- Getting all notes
- Deleting all notes
- Demo
- Conclusion
Prerequisites
To follow along with this tutorial, you need to:
- Understand the basics of Android development.
- Have basic knowledge of the Kotlin Programming language.
- Be conversant with Kotlin Coroutines.
- Be familiar with the concept of data/view binding.
Goal
This tutorial aims to explain what Paper database is, where to use it, the advantages and disadvantages of Paper DB, and how to implement it in Android.
What is a Paper database?
PaperDB is a NoSQL-like database for Java/Kotlin objects on Android that supports automatic schema migration. The goal of PaperDB is to provide an easy-to-use yet fast object storage solution for Android.
It allows users to use Java/Kotlin classes without any annotations, factory methods, or required class extensions.
Adding or removing fields from data classes is also no longer a chore because all data structure changes are handled automatically.
Advantages of Paper database
- Since it only takes column data, Paper databases consume less memory as compared to other databases.
- It is easy to implement.
- It is relatively faster than other databases.
Disadvantages of Paper database
- Huge amount of information cannot be stored within the paper database.
- Querying data from the database is a problem, especially categorical data.
When to use the Paper database
The Paper database is a great choice when:
- Saving minor details.
- The data to be stored consists of fields.
Getting started
To understand the concept of the Paper database better, we'll create a note app that adds notes to the database and displays them on a RecyclerView
.
Step 1 - Creating a new project
Launch Android Studio and create a new empty activity project. Give the project a descriptive name and select Kotlin as the preferred development language.
Step 2 - Adding dependencies
Go to build.gradle
(app level) and add paper database
and Kotlin Coroutines dependencies below:
// paper database
implementation 'io.github.pilgr:paperdb:2.7.2'
// coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
Step 3 - Creating the user interface
Since we need to insert data into the database and display it, we need two user interfaces, one to insert the data and the other to display the data.
They should look as shown below:
Displaying data in the user interface
Note that you must create the adapter and row item that is mapped to the RecyclerView for the data to be displayed.
Step 4 - Initializing Paper in an Application class
Create a NoteApp
class that inherits the Application
class. Override the onCreate
method and initialize Paper
database.
This should be done in the Main thread as follows:
class NoteApp: Application() {
override fun onCreate() {
super.onCreate()
Paper.init(this)
}
}
Step 5 - Creating the Model class
A model class defines the name and properties of the data object(s) held in a paper database. We list all of the essential data within the model class.
In this case, the model simply has two properties, note title, and note description:
data class Note(
Val title: String? = null,
Val description: String? = null
)
Step 6 - Inserting data into Paper database
Define an ArrayList
variable that will hold the notes:
private lateinit var noteList: ArrayList<Note>
Within the OnCreate
method, the noteList is checked and initialized to an empty ArrayList if it is null:
noteList = Paper.book().read("notes", ArrayList()) ?: ArrayList()
The Read
method is used to obtain data from the database while the Write
method is used to insert data into the database.
The read()
and write()
methods are thread sensitive and must be called outside of the UI thread. For this reason, we are going to perform insertion within a CoroutineScope
:
binding.btnSave.setOnClickListener {
// check if the input fields are empty
if (binding.etTitle.text.isEmpty() && binding.etDescription.text.isEmpty()){
return@setOnClickListener
}
CoroutineScope(Dispatchers.IO).launch {
// adding data to the array list
val note = Note(binding.etTitle.text.toString(), binding.etDescription.text.toString())
noteList.add(note)
// inserting data to paper database
Paper.book().write("notes", noteList)
}
// navigating back
startActivity(Intent(this, MainActivity::class.java))
finish()
}
Step 7 - Getting all Notes
The read()
method is used to obtain data from the database and must be called outside of the UI thread:
CoroutineScope(Dispatchers.Main).launch {
// checks if the array list has data
notes = Paper.book().read("notes", ArrayList<Note>()) ?: ArrayList<Note>()
// submitting data to the adapter
notesAdapter.submitList(notes)
// setting data to the recyclerView
binding.noteRecycler.adapter = notesAdapter
}
Step 8 - Deleting all Notes
To perform a delete operation, we use the delete()
function provided by the Paper library. This function accepts the key
as an input, checks if the provided key
has any value, and then performs the action.
The deletion method is as shown below:
Paper.book().delete("notes")
Demo
Upon running the app, you should expect to see the following:
Conclusion
In this tutorial, we have discussed what Paper database is, its advantages and disadvantages, where to use it, and how to implement Paper databases in Android.
You can get the full implementation of this tutorial on this GitHub repository.
Happy Coding!
Peer Review Contributions by: Eric Gacoki