You can check the logs how it doesn't stop making network requests:
D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "dribbble.com": No address associated with hostname
D/OkHttp: --> GET https://www.designernews.co/api/v2/stories?page=171
--> END GET
D/OkHttp: --> GET https://dribbble.com/search?q=Material%20Design&page=171&s=latest&per_page=12
--> END GET
D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "www.designernews.co": No address associated with hostname
D/OkHttp: --> GET https://api.producthunt.com/v1/posts?days_ago=170
--> END GET
D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "api.producthunt.com": No address associated with hostname
D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "dribbble.com": No address associated with hostname
D/OkHttp: --> GET https://www.designernews.co/api/v2/stories?page=172
--> END GET
D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "www.designernews.co": No address associated with hostname
D/OkHttp: --> GET https://dribbble.com/search?q=Material%20Design&page=172&s=latest&per_page=12
--> END GET
D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "dribbble.com": No address associated with hostname
D/OkHttp: --> GET https://api.producthunt.com/v1/posts?days_ago=171
--> END GET
...
...
Hi, I was trying to solve this issue. But I'm not sure which way.
We can make an Interceptor for OkHttpClient and cancel calls when there is no connection.
Pros:
Seems simple
Cons:
I ended up making call.cancel() inside the interceptor call and it still logs:
D/OkHttp: --> GET https://www.designernews.co/api/v2/stories?page=3
D/OkHttp: --> END GET
D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled
The old method of checking connection is Deprecated, so maybe new sync way or async way will make a trouble here.
We can use ConnectivityChecker only not unregister it for one time use. And then use it in InfiniteScrollListener and other places.
Pros:
Seems simple
Can use new async api I guess
Cons:
Check will be done in the UI thread since we will read livedata.value. Should be fast but it's UI thread...
Not very reusable, and every new call to the loadData, login or else should check connection itself.
Same as 2nd option but we will check connectionStatus.value in viewModel
Pros:
Still simple
Cons:
I don't know if it viewModels responsibility to do it
We can make standalone ConnectivityChecker that will be registered to the ConnectivityManager through the hole app lifecycle.
Then it can produce a livedata, that will listen to it through onActive/onInactive methods to be observed by UI, and it can give a connectionStatus to the deeper layers so that DataManager, Repositories, DataSources or UseCases will use it to call or not to call it and return Result.Error for these cases.
You can check the logs how it doesn't stop making network requests: