Fragmented - Android Developer Podcast cover image

Fragmented - Android Developer Podcast

147: Disposing RxJava 2 Streams with AutoDispose

Dec 31, 2018
15:22

In this short fragment episode, Donn explains how you can clean up your RxJava 2 streams and ensure no memory leaks are occurring by using the AutoDispose library from Uber.

Shownotes

Code Samples

Java

myObservable
	.map(...)
	.as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
	.subscribe(...)

Kotlin

myObservable
	.map(...)
	.autoDisposable(AndroidLifcycleScopeProvider.from(this))
	.subscribe(...)

With Scope Event Provided

myObservable
	.map(...)
	.autoDisposable(AndroidLifcycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY))
	.subscribe(...)

Testing

// File: CustomerService.kt
class CustomerService @Inject constructor(...) {
	lateinit var scopeProvider: ScopeProvider
}

// Usage in Fragment/Activity/etc
val service = CustomerService(...).apply {
	scopeProvider = AndroidLifecycleScopeProvider.from(this)
}

// Usage in Test
val service = CustomerService(...).apply {
	scopeProvider = TestScopeProvider.create()
}

Contact

Get the Snipd
podcast app

Unlock the knowledge in podcasts with the podcast player of the future.
App store bannerPlay store banner

AI-powered
podcast player

Listen to all your favourite podcasts with AI-powered features

Discover
highlights

Listen to the best highlights from the podcasts you love and dive into the full episode

Save any
moment

Hear something you like? Tap your headphones to save it with AI-generated key takeaways

Share
& Export

Send highlights to Twitter, WhatsApp or export them to Notion, Readwise & more

AI-powered
podcast player

Listen to all your favourite podcasts with AI-powered features

Discover
highlights

Listen to the best highlights from the podcasts you love and dive into the full episode