Skip to content

Popalay/RxRealm

Repository files navigation

Download Android Arsenal License

Rx Realm

Utilities for using RxJava with Realm

Getting Started

compile 'com.github.popalay:rx-realm:latest-version'

compile 'com.github.popalay:rx2-realm:latest-version'

Usage

From Java

    public Observable<List<MessageResponse>> getMessages() {
        return RxRealm.listenList(realm -> realm.where(MessageResponse.class)
                .findAllSorted(MessageResponse.CREATED_AT, Sort.DESCENDING));
    }

    public Single<List<MessageResponse>> loadMessages() {
        return mApi.messages()
                .observeOn(Schedulers.computation())
                .doOnSuccess(this::saveMessages);
    }
    
    private Completable saveMessages(List<MessageResponse> messages) {
        return RxRealm.doTransactional(realm -> {
                realm.where(MessageResponse.class).findAll().deleteAllFromRealm();
                realm.copyToRealmOrUpdate(messages);
            });
    }

From Kotlin

    fun getMessages(): Observable<List<MessageResponse>> {
        return RxRealm.listenList{it.where(MessageResponse::class.java)
                .findAllSorted(MessageResponse.CREATED_AT, Sort.DESCENDING)}
    }

    fun loadMessages(): Single<List<MessageResponse>> {
        return mApi.messages()
                .observeOn(Schedulers.computation())
                .doOnSuccess(this::saveMessages)
    }
    
    private saveMessages(messages: List<MessageResponse>): Completable {
        return RxRealm.doTransactional {
                it.where(MessageResponse::class.java).findAll().deleteAllFromRealm()
                it.copyToRealmOrUpdate(messages)
            }
    }

License

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.