Paper DB Android

Paper is an object-oriented storage option for android for java/kotlin.
it is a no-SQL database and supported by java and kotlin.
Papers provide a simple, fast, object container options in android.


Three most important features of Papers
  1. It can be used without any annotations, factory method, and class extension.  
  2. Adding and removing field to data class is not the developer's headache, it will handle automatically.
  3. Automatic schema migration support
Paper is a combination of the key/value data model and the object-oriented data model.
the paper used custom string as key and object as value.

Storing data in DB step by step-

Download Project source code from here

  1- add the dependency in Gradle

implementation 'io.paperdb:paperdb:2.7.1'

2- initialize Paper in Application class

public class ApplicationClass extends Application {
    @Override
public void onCreate() {
super.onCreate();

Paper.init(getApplicationContext());
}
}

3- Threading
    Paper.init() must be called in UI Thread;
    And other function, read, write, delete are thread-safe and must be called outside of the UI Thread.

4- Save data
  
Student student;
student = new Student("javaoneworld",
"Javaoneworld@gmail.com");
Paper.book().write("student",student);

5- Read data

List<Student> studentList1;
studentList1 = new ArrayList<>();
studentList1 = Paper.book().read("student");

6- use the default value if the object does not exist in the storage

List<Student> = Paper.book().read("student"
                               new ArrayList<>());

7- Delete data for one key

Paper.book().delete("student");

8- Removing all key for the given book

Paper.book().destroy();

9- Want to use the custom book

Paper.book("student1").write("student",student);
Paper.book("student2").write("student",student);
Paper.book("student3").write("student",student);
Paper.book("student4").write("student",student);

10- Return all key for an object in the book

List<String> allKeys = Paper.book().getAllKeys();

11- if you Want to Ignore some field from the saving process
     Use transient keyword with the field which you do not want to save

public transient String nikName = "Thor";
// nikName Won't be saved
 
12- if you want to change in your data model class, no worry, do changes what you want and it will manage automatically.
You can add and remove fields from the class.
  • The removed field will be ignored.
  • Newly added fields will have their default value.
Note- if you are changing only data type, it will not support.
example- in the start you have saved this model
class Student {
    public String name;
    public String email;
}
 
but after that, you want to change, see here

class Student {
    public String name;
    // public String email; removed field
    public Location location; // New field
}

the new field will initialize as the default value of null

13- Set Custom location for your file
 By default, Paper stored file path belongs to your apps package name

../you-app-package-name/files

To set it on your SD card, 

Paper.bookOn("/path/to/the/new/location")or
Paper.bookOn("path/to/the/new/location",
             "customer1_book")

//to create a custom book.

14- get the path


Paper.book().getPath();
Paper.book().getPath(key)



That's all about .
thank for being here.
#stayhappy #staycode

4 comments:

  1. Very nice...Keep it up to spread knowledge..👍

    ReplyDelete
    Replies
    1. i'm trying my best, to reach tech world by spreading my knowladge.

      Delete
  2. How to check if Object is not in it?

    ReplyDelete