Paper:Android 端的 NoSQL 數據存儲實現

jopen 9年前發布 | 16K 次閱讀 Paper Android開發 移動開發

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.baiduhome.net/lib/view/home/1448932716545

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!