All Projects → AleksanderMielczarek → ObservableCache

AleksanderMielczarek / ObservableCache

Licence: Apache-2.0 license
Library for caching Observables during orientation change

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to ObservableCache

Grox
Grox helps to maintain the state of Java / Android apps.
Stars: ✭ 336 (+1500%)
Mutual labels:  rxjava2, rxjava-android
RxKata
Learn Rx through Katas and exercises
Stars: ✭ 19 (-9.52%)
Mutual labels:  rxjava2, rxjava-android
Rxcache
简单一步,缓存搞定。这是一个专用于 RxJava,解决 Android 中对任何 Observable 发出的结果做缓存处理的框架
Stars: ✭ 377 (+1695.24%)
Mutual labels:  rxjava2, rxjava-android
Rxanime
Visualizer to understand RxJava operators
Stars: ✭ 261 (+1142.86%)
Mutual labels:  rxjava2, rxjava-android
Rx.observe
Transform any method to an Rx Observable ! (VIPER)
Stars: ✭ 34 (+61.9%)
Mutual labels:  rxjava2, rxjava-android
Nybus
NYBus (RxBus) - A pub-sub library for Android and Java applications
Stars: ✭ 283 (+1247.62%)
Mutual labels:  rxjava2, rxjava-android
Traceur
Easier RxJava2 debugging with better stacktraces
Stars: ✭ 502 (+2290.48%)
Mutual labels:  rxjava2, rxjava-android
AndroidVIP
Android project to experiment the VIPER approach using mosby, RxJava and dagger2
Stars: ✭ 21 (+0%)
Mutual labels:  rxjava2, rxjava-android
Rxdatabindings
RxJava2 extensions for Android Databindings library
Stars: ✭ 30 (+42.86%)
Mutual labels:  rxjava2, rxjava-android
Rxjava2 Operators Magician
你用不惯 RxJava,只因缺了这把钥匙 🔑 You are not used to RxJava, just because of the lack of this key.
Stars: ✭ 868 (+4033.33%)
Mutual labels:  rxjava2, rxjava-android
RxRetroAPICall
API call example using Retrofit and RxJava2
Stars: ✭ 16 (-23.81%)
Mutual labels:  rxjava2, rxjava-android
Rxandroidexamples
RxJava and RxAndroid complete beginner examples
Stars: ✭ 117 (+457.14%)
Mutual labels:  rxjava2, rxjava-android
RxAndroid-Examples
Learn RxJava by example
Stars: ✭ 32 (+52.38%)
Mutual labels:  rxjava2, rxjava-android
Freezer
A simple & fluent Android ORM, how can it be easier ? RxJava2 compatible
Stars: ✭ 326 (+1452.38%)
Mutual labels:  rxjava2, rxjava-android
android-online-course
Android Online Course
Stars: ✭ 22 (+4.76%)
Mutual labels:  rxjava2, rxjava-android
Android Mvp Architecture
This repository contains a detailed sample app that implements MVP architecture using Dagger2, GreenDao, RxJava2, FastAndroidNetworking and PlaceholderView
Stars: ✭ 4,360 (+20661.9%)
Mutual labels:  rxjava2, rxjava-android
Android Kotlin Mvp Architecture
This repository contains a detailed sample app that implements MVP architecture in Kotlin using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
Stars: ✭ 615 (+2828.57%)
Mutual labels:  rxjava2, rxjava-android
Mvpframes
整合大量主流开源项目并且可高度配置化的 Android MVP 快速集成框架,支持 AndroidX
Stars: ✭ 100 (+376.19%)
Mutual labels:  rxjava2, rxjava-android
Rxjavapriorityscheduler
RxPS - RxJavaPriorityScheduler - A RxJava Priority Scheduler library for Android and Java applications
Stars: ✭ 138 (+557.14%)
Mutual labels:  rxjava2, rxjava-android
Android Mvvm Architecture
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
Stars: ✭ 2,720 (+12852.38%)
Mutual labels:  rxjava2

Android Arsenal

ObservableCache

RxJava has become a standard in Android development. It's great until you have to deal with Android lifecycle. Normally you unsubscribe when view is destroyed and create new Observable after view is recreated. It's ok in most cases but sometimes there are actions, which cannot be done more than once i.e. HTTP Request which must be done once and Resposne must be received. In that cases Observables must be kept in place which lifecyle is different than destroyed view. This is where ObservableCache can be used. Library allows to cache Observable in global singleton map and retrieve same Observable after view is recreated. Internally library uses cache() for caching. Observables are automatically removed after onComplete.

RxJava 1.x

Observable Cache

Usage

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.ObservableCache:observable-cache-1:1.2.2'
}

Example

public class MainActivity extends AppCompatActivity {

    public static final String OBSERVABLE_CACHE_KEY_REQUEST = "observableRequest";

    private ObservableCache observableCache;
    private CompositeSubscription subscriptions;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        observableCache = LruObservableCache.getDefault();//get default singleton instance
        subscriptions = new CompositeSubscription();

    }

    public void performObservableAction() {
        Observable<String> observable = Observable.just("Test Action");
        observableAction(observable
                .compose(observableCache.cacheObservable(OBSERVABLE_CACHE_KEY_REQUEST)));//this line is responsible for caching observable
    }

    private void observableAction(Observable<String> testObservable) {
        subscriptions.add(testObservable
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {/*do sth with result*/});
    }

    @Override
    protected void onStart() {
        super.onStart();
        observableCache.<String>getObservable(OBSERVABLE_CACHE_KEY_REQUEST).ifPresent(this::observableAction);//retrieve observable from cache and perform action if observable exists
    }

    @Override
    protected void onStop() {
        super.onStop();
        subscriptions.clear();
    }
}

More information

  • caching Observable:
CacheableObservable<T> cachable = observableCache.cacheObservable(KEY);
  • caching Single:
CacheableSingle<T> cachable = observableCache.cacheSingle(KEY);
  • caching Completable:
CacheableCompletable<T> cachable = observableCache.cacheCompletable(KEY);
  • retrieve Observable:
ObservableFromCache<T> fromCache = observableCache.<T>getObservable(KEY);
  • retrieve Single:
SingleFromCache<T> fromCache = observableCache.<T>getSingle(KEY);
  • retrieve Completable:
CompletableFromCache<T> fromCache = observableCache.<T>getCompletable(KEY);
  • remove cached value:
boolean removed = observableCache.remove(KEY);
  • get new instance of cache:
ObservableCache observableCache = LruObservableCache.newInstance();
  • cache based on Map:
ObservableCache observableCache = MapObservableCache.newInstance();

Observable Cache Service

Using ObservableCache requires from developer writing unique keys for cached Observables. This can be error prone and that's why additional layer can be used. Instead of directly using ObservableCache and manually manipulating keys, Observable Cache Service generate classes from declared interfaces which internally assures that all keys are unique.

Usage

Add it in your root build.gradle at the end of repositories:

Add to the dependencies

dependencies {
    compile 'com.github.AleksanderMielczarek.ObservableCache:observable-cache-1-service:1.2.2'
    annotationProcessor 'com.github.AleksanderMielczarek.ObservableCache:observable-cache-1-service-processor:1.2.2'
}

Example

Previous example can be replaced with following implementation.

@ObservableCacheService
public interface CachedService {

    CacheableObservable<String> testObservable();

    ObservableFromCache<String> cachedTestObservable();

    boolean removeTestObservable();

}
public class MainActivity extends AppCompatActivity {

    private CachedService cachedService;
    private CompositeSubscription subscriptions;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ObservableCache observableCache = LruObservableCache.getDefault();
        ObservableCacheService observableCacheService = new ObservableCacheService(observableCache);
        cachedService = observableCacheService.createObservableCacheService(CachedService.class);
        subscriptions = new CompositeSubscription();
    }

    public void performObservableAction() {
        Observable<String> observable = Observable.just("Test Action");
        observableAction(observable
                .compose(cachedService.testObservable()));//this line is responsible for caching observable
    }

    private void observableAction(Observable<String> testObservable) {
        subscriptions.add(testObservable
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {/*do sth with result*/});
    }

    @Override
    protected void onStart() {
        super.onStart();
        cachedService.cachedTestObservable().ifPresent(this::observableAction);//retrieve observable from cache and perform action if observable exists
    }

    @Override
    protected void onStop() {
        super.onStop();
        subscriptions.clear();
    }
}

More information

Keys are generated based on method names:

  • key value is based on method name that takes 0 arguments and returns CacheableObservable, CacheableSingle or CacheableCompletable
  • method that retrieves value from cache takes 0 arguments and returns ObservableFromCache, SingleFromCache or CompletableFromCache. Name of this method must be the same as method for caching values + word 'cached':
    • cache: 'testObservable()', retrieve: 'cachedTestObservable()'
    • cache: 'testObservable()', retrieve: 'testCachedObservable()'
    • cache: 'testObservable()', retrieve: 'testObservableCached()'
  • method that removes value from cache takes 0 arguments and returns boolean.Name of this method must be the same as method for caching values + word 'remove':
    • cache: 'testObservable()', remove: 'removeTestObservable()'
    • cache: 'testObservable()', remove: 'testRemoveObservable()'
    • cache: 'testObservable()', remove: 'testObservableRemove()'

ProGuard

-keep class com.github.aleksandermielczarek.observablecache.service.ObservableCacheServiceCreatorImpl

RxJava 2.x

RxJava 2 usage is very similar to RxJava 1.

New types:

  • caching Flowable:
CacheableFlowable<T> cachable = observableCache.cacheFlowable(KEY);
  • caching Maybe:
CacheableMaybe<T> cachable = observableCache.cacheMaybe(KEY);
  • retrieve Flowable:
FlowableFromCache<T> fromCache = observableCache.<T>getFlowable(KEY);
  • retrieve Maybe:
MaybeFromCache<T> fromCache = observableCache.<T>getMaybe(KEY);

Observable Cache

Usage

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Add the dependency

dependencies {
    compile 'com.github.AleksanderMielczarek.ObservableCache:observable-cache-2:1.2.2'
}

Observable Cache Service

Usage

Add it in your root build.gradle at the end of repositories:

Add to the dependencies

dependencies {
    compile 'com.github.AleksanderMielczarek.ObservableCache:observable-cache-2-service:1.2.2'
    annotationProcessor 'com.github.AleksanderMielczarek.ObservableCache:observable-cache-2-service-processor:1.2.2'
}

ProGuard

-keep class com.github.aleksandermielczarek.observablecache2.service.ObservableCacheServiceCreatorImpl

Changelog

1.2.2 (2017-07-19)

  • make values from cache constructors public

1.2.1 (2017-07-18)

  • add static factory methods to values from cache

1.2.0 (2017-03-06)

  • simplify API

1.1.2 (2017-03-05)

  • change ifPresent method to void

1.1.1 (2017-03-03)

  • fix issue that does not remove Single and Maybe from cache

1.1.0 (2017-02-10)

  • add RxJava 2.x support
  • rename RxJava 1.x modules

1.0.0 (2016-11-06)

  • add generator for caching interface

License

Copyright 2016 Aleksander Mielczarek

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.
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].