Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using this with RxJava2 #6

Open
andreyrd opened this issue Dec 18, 2017 · 7 comments
Open

Using this with RxJava2 #6

andreyrd opened this issue Dec 18, 2017 · 7 comments

Comments

@andreyrd
Copy link

Not sure if this can be used together with RxJava2CallAdapterFactory, since then I can't use SmartCall in my interface. I have to use Observable, which means the SmartCacheFactory will not work.

@andreyrd
Copy link
Author

andreyrd commented Dec 27, 2017

Here's the simple implementation (in Kotlin) I came up with for wrapping a SmartCall into an Observable. I could potentially create a Factory for it, just not sure how to yet. Depending on if I have time, I'll do that and open a PR! But for now this works for us:

class SmartCacheObservable<T>(private val call: SmartCall<T>): Observable<T>() {

    override fun subscribeActual(observer: Observer<in T>) {
        val call = call.clone()

        call.enqueue(object : Callback<T> {

            override fun onResponse(call: Call<T>?, response: Response<T>) {
                val body = response.body()

                if (body != null) {
                    observer.onNext(body)
                } else {
                    observer.onError(Throwable("Could not read response body!"))
                }

                // The current version of SmartCache will set the url to "http://localhost/" for
                // cached responses, we can use that to wait until the real network request
                // completes to call `observer.onComplete()`
                if (response.raw().request().url().host() != "localhost") {
                    observer.onComplete()
                }
            }

            override fun onFailure(call: Call<T>?, error: Throwable) {
                observer.onError(error)
                observer.onComplete()
            }
        })
    }
}

@bhoopendrayash
Copy link

bhoopendrayash commented Feb 8, 2018

Hi , andreyrd I am using retrofit 2 with RX java , i Need to show data from cache before network call , As i am using rx java so here i can not use SmartCacheFactory . do you have any solution for that

@andreyrd
Copy link
Author

andreyrd commented Feb 9, 2018

Hey, @bhoopendrayash, I believe you can just use a Java version of my SmartCall wrapper. I'll try to find time later today to convert and test it in Java, unless someone else does it first.

@bhoopendrayash
Copy link

bhoopendrayash commented Feb 9, 2018

Hey @andreyrd , Thank you so much for your quick response , i will try to convert your SmartCacheObservable class to java

@dula34
Copy link

dula34 commented Jan 24, 2019

@bhoopendrayash have you found a solution?

@FreedomChuks
Copy link

hello i too is facing this issue how can it be used with rxjava

@fikisipi
Copy link
Owner

fikisipi commented Apr 26, 2021

@gzodx @dula34 @andreyrd Hey, I've released a new version with Retrofit 2.9, and some niceties: a new demo app, a function for checking if current callback is loaded from disk, clearing cache and a way to filter for which requests should be cached.

Change the repo username to fikisipi and use SmartCache:2.9.0 from JitPack. I don't have experience with RxJava but I'd love to see a PR or help with adding that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants