All Projects → pakoito → RxPaper

pakoito / RxPaper

Licence: MIT License
NoSQL storage with RxJava bindings [STABLE]

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to RxPaper

RXBus
RX based bus with lifecycle based queuing support
Stars: ✭ 53 (-39.77%)
Mutual labels:  rxjava
reactor-go
A golang implementation for reactive-streams.
Stars: ✭ 48 (-45.45%)
Mutual labels:  rxjava
rx-property-android
Bindable and observable property for Android Data Binding
Stars: ✭ 76 (-13.64%)
Mutual labels:  rxjava
catchflicks
🎬 Kitchen sink project for learning android concepts 🎬
Stars: ✭ 12 (-86.36%)
Mutual labels:  rxjava
rxify
Now: RxJava Playground, Previous: Demo for the talk at DroidconIN 2016, Droidcon Boston 2017 and Codelab for GDG January Meetup
Stars: ✭ 59 (-32.95%)
Mutual labels:  rxjava
BihuDaily
高仿知乎日报
Stars: ✭ 75 (-14.77%)
Mutual labels:  rxjava
WanAndroid
💪 WanAndroid应用,持续更新,不断打造成一款持续稳定, 功能完善的应用
Stars: ✭ 50 (-43.18%)
Mutual labels:  rxjava
modern-android
Modern Android Project Skeleton
Stars: ✭ 17 (-80.68%)
Mutual labels:  rxjava
RxJava-Codelab
Codelab project for demonstration of RxJava features
Stars: ✭ 44 (-50%)
Mutual labels:  rxjava
Readhub
Readhub AndroidClient
Stars: ✭ 40 (-54.55%)
Mutual labels:  rxjava
rxbus2
Listen and handle event ,based on RxJava.
Stars: ✭ 32 (-63.64%)
Mutual labels:  rxjava
RxRealm
Utilities for using RxJava with Realm
Stars: ✭ 23 (-73.86%)
Mutual labels:  rxjava
DaMaiProject
大麦界面,实现多种方式网络访问、数据缓存
Stars: ✭ 24 (-72.73%)
Mutual labels:  rxjava
rxkotlin-jdbc
Fluent RxJava JDBC extension functions for Kotlin
Stars: ✭ 27 (-69.32%)
Mutual labels:  rxjava
StudyNotes
🔥我只是在假装努力,你却在真正成长
Stars: ✭ 13 (-85.23%)
Mutual labels:  rxjava
Android-Learning-Resources
My curated list of resources for learning Android Development.
Stars: ✭ 24 (-72.73%)
Mutual labels:  rxjava
AndroidGo
Android、Flutter 开发者帮助 APP。包含事件分发、性能分析、Google Jetpack组件、OkHttp、RxJava、Retrofit、Volley、Canvas绘制以及优秀博文代码案例等内容,帮助开发者快速上手!
Stars: ✭ 30 (-65.91%)
Mutual labels:  rxjava
ProxerAndroid
The official Android App of Proxer.Me
Stars: ✭ 105 (+19.32%)
Mutual labels:  rxjava
Binder
An Annotation processor that allows binding two classes with each other, where the first class can listen to the updates of the second class ... ideal for MVVM and similar patterns
Stars: ✭ 21 (-76.14%)
Mutual labels:  rxjava
RxRetroAPICall
API call example using Retrofit and RxJava2
Stars: ✭ 16 (-81.82%)
Mutual labels:  rxjava

RxPaper

RxPaper is an RxJava wrapper for the cool Paper library. It's a clean rewrite of the original RxPaper library by César Ferreira.

For the RxJava 2 version please go to RxPaper2.

Paper icon

Rationale

Sometimes you need storage for arbitrary objects on disk, but do not want to store them in a relational database with all the associated problems: writing ORMs, composing the queries, updating scripts. For this purpose NoSQL data storages were created: schemaless document repositories where to store arbitrary data that's not structured.

RxPaper allows access to Paper, which is a NoSQL data storage for Android that allows you to save arbitrary objects in system files called Books. Serialization and deserialization is done using efficient Kryo.

The Paper/Kryo combination supports some partial data structure changes. Check Paper's README for more information.

Updating from 1.X

As PaperDb 2.0 has updated from Kryo 3 to Kryo 4, the internal representation model has changed. PaperDb deals with these changes internally, so the migration should be transparent. If you find any data compatibility bug, please create a ticket.

Usage

Object model handling

RxPaper is subject to the same restrictions as the current version of Paper when the library was last updated. As of Paper 1.5, the library can work with empty constructors and all-arg constructors. Some other combinations would need to be tested by the library user.

I personally recommend using immutable objects, as it makes data handling way simpler on both sides. An immutable object has an all-args constructor, doesn't allow any null fields, and keeps all fields public and final. Like any other dto in Java, it is recommended to implement your own version of equals, hashCode and toString.

As of Paper 1.5 you can also add your own serializers by calling Paper.addSerializer(). Partial structure changes are supported too, as described on Paper's README.

Threading

All operations are run on the Scheduler provided on the constructor, or Schedulers.io() by default. When subscribing to them, specially if using the data to be applied to UI; it's recommended to use the operator observeOn(Scheduler) to see the changes on any desired thread, i.e. Android's main thread.

Initialization

Before the library is usable it requires initializing the underlying Paper library. You only have to initialize RxPaper by calling:

RxPaperBook.init(context);

Working on a book

RxPaper works on books, and each is a folder on the system. A book is only opened and closed on an operation, but you can check the Paper repository for specifics. To make sure no operations are done on the main thread, any operations done on a book can be executed on one Scheduler provided in the constructor. To create an instance of RxPaper the library provides several flavours.

RxPaperBook.with();

Works with the default book, and executes operations on Schedulers.io().

RxPaperBook.with(Schedulers.newThread());

Works with the default book, and executes operations on any provided scheduler.

RxPaperBook.with("my_book_name");

Works with a custom book with the provided id/name, and executes operations on Schedulers.io().

RxPaperBook.with("my_book_name", Schedulers.newThread());

Works with a custom book with the provided id/name, and executes operations on any provided scheduler.

Writing a value

Write is a Completable operation, a subset of Observable<T> without a return value, just success/error. Completables can be converted back to Observables by using the operator toObservable().

RxPaperBook book = RxPaperBook.with("my-book");
Completable write = book.write(key, value);
// Because RxJava is lazily evaluated, the operation is not executed until the Observable is subscribed.
write.subscribe(new Completable.CompletableSubscriber() {
            @Override
            public void onCompleted() {
                // Operation suceeded
            }

            @Override
            public void onError(Throwable e) {
                // Operation failed
            }

            @Override
            public void onSubscribe(Subscription d) {
                // Called once on creation
            }
        });

Every key written is stored as a file on the system under the folder specified by the book.

Reading a value

Reading is a Single<T> operation, a subset of Observable<T> that returns just a single element and then completes. Singles can be converted back to Observables by using the operator toObservable(). Reading comes in two flavours:

Single<ComplexObject> read = book.read(key);
read.subscribe(new SingleSubscriber<ComplexObject>() {
            @Override
            public void onSuccess(ComplexObject value) {
                // Operation succeeded and returned a value
            }

            @Override
            public void onError(Throwable error) {
                // Operation failed
            }
        });

ComplexObject defaultValue = new ComplexObject();
Single<ComplexObject> readOrDefault = book.read(key, defaultValue);
readOrDefault.subscribe(new SingleSubscriber<ComplexObject>() {
            @Override
            public void onSuccess(ComplexObject value) {
                // Operation succeeded and returned a value
            }

            @Override
            public void onError(Throwable error) {
                // Operation failed
            }
        });

read(key) fails with IllegalArgumentException if the key is not found. read(key, defaultValue) returns a defualt value if the key is not found.

If the subscriber is not of the same type as the value stored expect a ClassCastException.

Make sure to read the rules on how object models are handled on the section above.

Observing changes on a key

All write operations are naively forwarded into a PublishSubject<?> by default, which makes it possible to observe all changes for a specific key. Observing is an Observable<T> operation that never completes.

Observable<ComplexObject> observe = book.observe(key, ComplexObject.class);
observe.subscribe(new Subscriber() { /* ... */ });

Observe filters on both the key and the type. Another version of observe that filters only on key and casts any values unsafely is provided under the name observeUnsafe(). It's recommended to use it with strict care.

Exists

Exists is a Single<Boolean> operation that returns true if the key is on the current book, or false otherwise.

Single<Boolean> exists = book.exists(key);
exists.subscribe(new SingleSubscriber<Boolean>() { /* ... */ });

Delete

Delete is a Completable operation. Deletes data stored for a key on the current book. It will still succeed even if the key is not found.

Completable delete = book.delete(key);
delete.subscribe(new Completable.CompletableSubscriber() { /* ... */ });

Keys

Keys is a Single<List<String>> operation that returns a list of all keys stored on the current book.

Single<List<String>> keys = book.keys();
exists.subscribe(new SingleSubscriber<List<String>>() { /* ... */ });

Destroy

Destroy is a Completable operation that deletes all keys and values on the current book.

Completable destroy = book.destroy();
destroy.subscribe(new Completable.CompletableSubscriber() { /* ... */ });

Distribution

Add as a dependency to your build.gradle

    repositories {
        ...
        maven { url "https://jitpack.io" }
        ...
    }
    
    dependencies {
        ...
        compile 'com.github.pakoito:RxPaper:2.1.0'
        ...
    }

or to your pom.xml

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
    
    <dependency>
        <groupId>com.github.pakoito</groupId>
        <artifactId>RxPaper</artifactId>
        <version>2.1.0</version>
    </dependency>

License

Copyright (c) 2016 pakoito & 2015 César Ferreira

The MIT License (MIT)

See LICENSE.md

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].