All Projects → WaylonBrown → LifecycleAwareRx

WaylonBrown / LifecycleAwareRx

Licence: Apache-2.0 license
Make your RxJava2 streams life-cycle aware with Android Architecture Components.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to LifecycleAwareRx

Android Architecture Components Kotlin
Clean code App with Kotlin and Android Architecture Components
Stars: ✭ 23 (-30.3%)
Mutual labels:  android-architecture, android-architecture-components
Armscomponent
📦 A complete android componentization solution, powered by MVPArms (MVPArms 官方快速组件化方案).
Stars: ✭ 1,664 (+4942.42%)
Mutual labels:  android-architecture, rxjava2
Android Clean Architecture Example
Yet another Android clean architecture example using RxJava and Room.
Stars: ✭ 37 (+12.12%)
Mutual labels:  android-architecture, rxjava2
KTAndroidArchitecture
A Kotlin android architecture with Google Architecture Components
Stars: ✭ 33 (+0%)
Mutual labels:  android-architecture, android-architecture-components
Android Mvvm Architecture
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
Stars: ✭ 2,720 (+8142.42%)
Mutual labels:  android-architecture, rxjava2
Android Mvp Architecture
This repository contains a detailed sample app that implements MVP architecture using Dagger2, GreenDao, RxJava2, FastAndroidNetworking and PlaceholderView
Stars: ✭ 4,360 (+13112.12%)
Mutual labels:  android-architecture, rxjava2
Android Kotlin Mvp Clean Architecture
Clean architecture blueprint using Kotlin and MVP pattern.
Stars: ✭ 105 (+218.18%)
Mutual labels:  android-architecture, rxjava2
Android Mvp Mvvm Flytour
🔥🔥🔥 FlyTour是Android MVVM+MVP+Dagger2+Retrofit+RxJava+组件化+插件组成的双编码架构+双工程架构+双语言Android应用开发框架,通过不断的升级迭代该框架已经有了十个不同的版本,5.0之前工程架构采用gradle配置实现组件化,5.0之后的工程架构采用VirtualAPK实现了插件化,5.0之前采用Java编码实现,5.0之后采用Kotlin编码实现,编码架构由MVVM和MVP组成,工程架构和编码架构及编码语言开发者可根据自己具体的项目实际需求去决定选择使用,该框架是Android组件化、Android插件化、Android MVP架构、Android MVVM架构的集大成者,帮助你快速的搭建自己的App项目开发框架,以便把主要的精…
Stars: ✭ 2,948 (+8833.33%)
Mutual labels:  android-architecture, rxjava2
Cw Androidarch
Source Code for the Book "Android's Architecture Components"
Stars: ✭ 213 (+545.45%)
Mutual labels:  android-architecture, android-architecture-components
Android Developer Roadmap
Android Developer Roadmap - A complete roadmap to learn Android App Development
Stars: ✭ 2,170 (+6475.76%)
Mutual labels:  android-architecture, rxjava2
Roxie
Lightweight Android library for building reactive apps.
Stars: ✭ 441 (+1236.36%)
Mutual labels:  android-architecture, rxjava2
github-commit-browser
A blog companion sample project that demonstrates saving UI state after process death on Android utilizing the community established 3rd party libraries
Stars: ✭ 55 (+66.67%)
Mutual labels:  android-architecture, android-architecture-components
Mvvmarms
Android MVVM Architecture Components based on MVPArms and Android Architecture Components.
Stars: ✭ 425 (+1187.88%)
Mutual labels:  android-architecture, rxjava2
News
A sample News 🗞 app built using Modern Android Development [Architecture Components, Coroutines, Retrofit, Room, Kotlin, Dagger]
Stars: ✭ 774 (+2245.45%)
Mutual labels:  android-architecture, android-architecture-components
Android Architecture Components
The template project that uses Android Architecture Components with Repository pattern. The simple app that uses awesome Fuel library instead of Retrofit for perfoming HTTP request. The app also persists data using the Room library and display data in RecyclerView.
Stars: ✭ 329 (+896.97%)
Mutual labels:  android-architecture, android-architecture-components
Aacpaginginfinitescrollingwithnetworksample
AAC Paging Infinite Scrolling With Network Sample
Stars: ✭ 74 (+124.24%)
Mutual labels:  android-architecture, android-architecture-components
repolib-android
RepoLib Rx - Android
Stars: ✭ 13 (-60.61%)
Mutual labels:  android-architecture, rxjava2
UTair-MVP-Sample
Android Clean Architecture + MVP Sample written in Kotlin
Stars: ✭ 27 (-18.18%)
Mutual labels:  android-architecture, rxjava2
Mvp Architecture Components
This is a sample project, showing the connection between Android Architecture Components and MVP pattern.
Stars: ✭ 143 (+333.33%)
Mutual labels:  android-architecture, android-architecture-components
RestaurantsExplorer
Android application build with MVVM Pattern, using Zomato API to enable search cities arround the world and display the city restaurants on a map.
Stars: ✭ 32 (-3.03%)
Mutual labels:  rxjava2, android-architecture-components

Lifecycle-aware RxJava2 with Android's new Lifecycle API

With the introduction to Android Architecture Components, Google added LiveData to their Lifecycle component which is a very simple observable that is "lifecycle-aware". It takes advantage of a new API in Activities and Fragments, LifecycleOwner, which has lifecycle state callbacks and ensures that you only observe new values returned by the observable once the Activity/Fragment is active and destroys itself once the Activity/Fragment is destroyed automatically. Goodbye memory leaks and prematurely accessed views.

LifecycleAwareRx is a lightweight library that lets RxJava leverage the same API, making your reactive streams life-cycle aware with Android's first-party lifecycle API.

The two main features you get out of using LifecycleAwareRx:

  1. As soon as your Activity/Fragment is destroyed, your RxJava2 stream ends and stops emitting items and the LifecycleOwner reference is removed ensuring you don't have any memory leaks or access to views from your stream after they've been destroyed.
  2. Your stream won't subscribe to its Observer - which has the callbacks you care about to update the views - until the Activity/Fragment is active. The stream will still do its work beforehand, but it will cache any items that are emitted and only subscribe to the Observer once the ActivityFragment is active and its views are ready and emit each of the cached items in-order.

Right now this is with the use of Google's new LifecycleActivity and LifecycleFragment, but very soon they are going to bake the LifecycleOwner API directly into the support library's Activity/Fragment, meaning you'll be able to use this library with your current Activities and Fragments with no changes.

How to use

To reiterate from above, for now you first need to have your Activities and Fragments that want to use this extend LifecycleActivity and LifecycleFragment.

Then it's as simple as

getMyObservable()
	.compose(LifecycleBinder.bind(this, new DisposableObserver<MyObject>() {
		@Override
		public void onNext(final <MyObject> myObject) {
			updateMyViewsWithData(myObject); // You can safely update your views here, knowing the Activity/Fragment isn't destroyed
		}
		
		@Override
		public void onError(final Throwable e) {
			updateMyViewsWithError(e); // Same here, except you need to do a state check here with Singles! See the note under "Singles are special" as to why.
		}

		@Override
		public void onComplete() {
		}
	}));
		

where

  • getMyObservable() could also instead be a Single or Maybe
  • this is your Activity or Fragment
  • DisposableObserver could instead be a regular Observer, or could otherwise be the correct type for your reactive type such as SingleObserver, DisposableSingleObserver, etc.

This automatically stops emitting items and throws away the LifecycleOwner (your Activity or Fragment) reference as soon as it hits onDestroy(). It also waits until your Activity/Fragment has its onStart() called before emitting any items, ensuring your views are ready to be updated.

Because items are cached if they aren't yet ready to be emitted then are all emitted in-order once the onStart() is called, with Observables you can use takeLast(1) before you call compose() if you only want the last item cached to be emitted at that point instead of all items cached to be emitted.

Singles are special

Singles can't be empty, they either represent a success or a failure. Because of this, if the onDestroy() is called before the Single emits its item, it will emit an onError() with a NoSuchElementException(). Because of this, you need to make sure to do the following check if you want to update your views in onError() if the stream is a Single (Observables and Maybes don't need this check).

// This is within your Single's Observer that is uses inside of the compose()
@Override
public void onError(final Throwable e) {
	if (getLifecycle().getCurrentState() != State.DESTROYED) {
		updateMyViewsWithError(e);
	}
}

Add to your project

Add the jitpack repository if you haven't already to your top-level project build.gradle.

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

Then add the dependency to your project's build.gradle.

dependencies {
  	compile 'com.github.WaylonBrown:LifecycleAwareRx:0.2'	// Check the JitPack badge at the top of the README for the latest version.
}

New library features

I am very open to any new feature suggestions, so please add Github issues if there's something that should be added.

How's this different from RxLifecycle

I'm only adding this section because several have asked the same question :) I wouldn't have made this if it were the same thing, it fulfills a different but similar need.

  1. The most important difference is that it uses Android's new first-party Lifecycle API. This means that you extend your Activity or Fragment as usual rather than extending a new class created just by this library (well, for now using Android's new LifecycleActivity and LifecycleFragment, but this functionality will soon be added straight into the support library's Activity/Fragment). Also, the complexity is far simpler than RxLifecycle since their API gives us observers for observing lifecycle state changes.
  2. On top of just completing the stream once the Activity/Fragment is destroyed, it also takes care of deferring emitting items until the Activity/Fragment is active, as is done with Android's new LiveData.
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].