Paper:Android 端的 NoSQL 数据存储实现

jopen 8年前

Paper是一个快速的Android 端NoSQL数据存,让你能够使用高效的Kryo序列化来保存/恢复Java对象并自动处理数据结构的变化。

Paper icon

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();

项目主页:http://www.open-open.com/lib/view/home/1448932716545