G-Rex


Source link: https://github.com/Gridstone/RxStore

RxStore

A tiny library that assists in saving and restoring objects to and from disk using RxJava, and observing changes over time.

This library now targets RxJava2. If you're using RxJava1 then take a look at a version 5.1.1.

Details

RxStore is a simple persistence framework for those already making use of RxJava2 in their projects. There are many occasions where you don't need the complexity introduced by a database; you just require a simple put/get API.

We have found this particularly useful on Android, where there are many options, but none of them quite right...

  • Simple key/value pair? SharedPreferences makes that simple.
  • Elaborate interconnected data sets? SQLite can help you.
  • Everything else? We just want to put and get objects from disk with minimal overhead.

By design, RxStore lets you use whatever serialization format you prefer, so long as you provide a valid Converter. Converters for Moshi, Gson and Jackson are provided out of the box, and pull requests for more are always welcome!

Leaning on RxJava, RxStore can help alleviate some threading concerns when reading and writing to disk, and allows for some pretty nifty method chaining once the operation completes. It also lets you observe changes as you write new values into stores.

Usage

Creating Stores

There are two kinds of stores:

  • ValueStore lets you write, read, and observe changes to a single value you want to persist.
  • ListStore does the same but for many values, and has convenience methods for adding and removing individual items in the list.

Say we have a model class called Person

public final class Person {

public final String name;
public final int age; 
}

To persist a single Person, we must first create a ValueStore.

ValueStore<Person> store = RxStore.value(file, converter, Person.class);

In addition to the type we must also provide a File and a Converter. The File gives the object a place to live on disk, and the Converter dictates how it's saved and restored. You can make your own Converter or use one we prepared earlier.

Storing Data

There are two ways we can add a Person to our store: store.put(person) or store.observePut(person). put() is a fire-and-forget method that will asynchronously write the value to disk. observePut() returns an RxJava Single that must be subscribed to in order for the write operation to begin. This is useful when incorporating the write operation into a chain, or would like to know when a write operation has completed.

Perhaps you're making use of Square's Retrofit. You could download and persist data in a single Rx chain.

webServices.getPerson()
  .flatMap((person) -> store.observePut(person))
  .subscribe((person) -> {

 // Do something with newly downloaded and persisted person.
  
}
);

ListStore is useful if you wanted to store many people. In addition to put(people) it also has some handy methods such as add(person) and remove(person).

Retrieving Data

When retrieving from a ValueStore we can use store.get() or store.blockingGet(). The former returns a Maybe, as there may not be a current value. The latter blocks until the disk read and deserialization is complete, and returns a nullable value.

ListStore behaves slightly differently. get() returns a Single, as empty stores can be represented by an immutable empty List. blockingGet() will always return a non-null List.

Observing Data

Another handy trick is to observe a store change over time. Calling store.observe() will give you an Rx Observable. This Observable will immediately deliver the current value of the store upon subscription, and will then deliver updated values if changes occur in onNext().

It's worth noting that valueStore.observe() does not return Observable<T>, but rather Observable<ValueUpdate<T>>. This is because the store cannot use null to represent the absence of a value, and must wrap the update in a non-null object.

listStore.observe() however does return Observable<List<T>>, as an empty ListStore can be represented by an immutable empty List.

Kotlin

If you're working in Kotlin, there are also two convenient functions provided in the rxstore-kotlin artifact that make use of reified type parameters. This removes the need to pass the Type in the store initialisation methods.

val personStore = storeProvider.valueStore<Person>(file, converter) val peopleStore = storeProvider.listStore<Person>(file, converter)

Download

All artifacts are up on Maven Central.

For the base library

compile 'au.com.gridstone.rxstore:rxstore:6.0.0'

For the kotlin convenience functions

compile 'au.com.gridstone.rxstore:rxstore-kotlin:6.0.0'

For the Moshi converter

compile 'au.com.gridstone.rxstore:converter-moshi:6.0.0'

For the Gson converter

compile 'au.com.gridstone.rxstore:converter-gson:6.0.0'

For the Jackson converter

compile 'au.com.gridstone.rxstore:converter-jackson:6.0.0'

License

Copyright 2017 GRIDSTONE  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

A simple Floating Action Button that shows an anchored Navigation View and was inspired by Menu Material Fixed created by Tommaso Poletti

This is an extension for Android Lint. Rock your Android.

A simple Android library to implement a number counter with increment and decrement buttons.

An Android service to retrieve GPS locations and route stats using RxJava.

A Simplenote client for Android.

Detailed CardView that displays an action title, description, and buttons to initiate that action.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes