Paper:Android 端的 NoSQL 數據存儲實現
Paper是一個快速的Android 端NoSQL數據存,讓你能夠使用高效的Kryo序列化來保存/恢復Java對象并自動處理數據結構的變化。
Add dependency
compile 'io.paperdb:paperdb:1.1'
Initialize Paper
Should be initialized one time in onCreate() in Application or Activity.
Paper.init(context);
It's OK to call it in UI thread. All other methods should be used in background thread.
Save
Save data object. Your custom classes must have no-arg constructor. Paper creates separate data file for each key.
Paper.book().write("city", "Lund"); // Primitive Paper.book().write("task-queue", queue); // LinkedList Paper.book().write("countries", countryCodeMap); // HashMap
Read
Read data objects. Paper instantiates exactly the classes which has been used in saved data. The limited backward and forward compatibility is supported. See Handle data class changes.
String city = Paper.book().read("city"); LinkedList queue = Paper.book().read("task-queue"); HashMap countryCodeMap = Paper.book().read("countries");
Use default values if object doesn't exist in the storage.
String city = Paper.book().read("city", "Kyiv"); LinkedList queue = Paper.book().read("task-queue", new LinkedList()); HashMap countryCodeMap = Paper.book().read("countries", new HashMap());
Delete
Delete data for one key.
Paper.book().delete("countries");
Completely destroys Paper storage. Requires to callPaper.init()before usage.
Paper.book().destroy();
Use custom book
You can create custom Book with separate storage using
Paper.book("custom-book")...;
Each book is located in separate file folder.
Get all keys
Returns all keys for objects in the book.
List<String> allKeys = Paper.book().getAllKeys();