Comments (7)
Thanks for the update.
I will try to look into this problem, but at this moment I'm not really sure if it's related to the library code or the Android OS.
From your log I see, crash is at this line:
when registering network callback (manager.registerNetworkCallback(request, networkCallback);
), so maybe we need to introduce a state, which will keep an information whether this callback was already registered or not.
I will try to check it later.
Regards,
Piotr
from reactivenetwork.
For those running into this issue and have minimum SDK 24, the fix is to simply do this, which is what we ended up doing, no need for this library:
@Singleton
class NetworkConnectivityMonitor
@Inject
constructor(context: Context) : INetworkConnectivityMonitor, LifecycleObserver {
private val appStateSubject = BehaviorSubject.create<AppState>()
private val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
private val isConnectedSubject = BehaviorSubject.create<Boolean>()
override val isConnectedObservable = isConnectedSubject.hide()
private val callback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
isConnectedSubject.onNext(true)
}
override fun onLost(network: Network) {
isConnectedSubject.onNext(false)
}
override fun onLosing(network: Network, maxMsToLive: Int) {
isConnectedSubject.onNext(false)
}
override fun onUnavailable() {
isConnectedSubject.onNext(false)
}
override fun onBlockedStatusChanged(network: Network, blocked: Boolean) {
isConnectedSubject.onNext(!blocked)
}
}
init {
appStateSubject.subscribe { appState ->
when (appState) {
AppState.FOREGROUND -> connectivityManager.registerDefaultNetworkCallback(callback)
AppState.BACKGROUND -> connectivityManager.unregisterNetworkCallback(callback)
}
}
}
init {
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onForeground() {
appStateSubject.onNext(AppState.FOREGROUND)
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onBackground() {
appStateSubject.onNext(AppState.BACKGROUND)
}
}
enum class AppState {
FOREGROUND,
BACKGROUND
}
interface INetworkConnectivityMonitor {
val isConnectedObservable: Observable<Boolean>
}
This will listen for connectivity changes when the app is in foreground.
At this point all you need to do if you want to listen for connectivity changes you just subscribe to isConnectedObservable
. You can have as many subscribers as you want, it uses a Subject under the scenes so we only registerDefaultNetworkCallback
once, regardless of the amount of subscribers.
Hope it's useful.
from reactivenetwork.
Hi,
Thanks for reporting this issue. It seems to be related to the specific version of the Android OS. It looks like Android is blocking situation, when there is too many network requests. I also don't know how many is "too many" Catching this exception won't solve the problem. I think, in this case you should use observe network only or limit somehow these network checks. Also ensure if you are disposing this disposable in the activity/service lifecycle. If it's not disposed, problems like that may occur very often.
I will read about this problem and verify if it's possible to handle that properly within the library.
Regards,
Piotr
from reactivenetwork.
Hi @pwittchen
Thank you so much your message.
I'm going to fix my function using firstOrError() as you mention.
fun checkInternetConnectivity(context: Context): Single<Boolean> =
ReactiveNetwork.observeNetworkConnectivity(context)
.flatMapSingle { connectivity ->
@Suppress("DEPRECATION")
if (connectivity.state() === NetworkInfo.State.CONNECTED) {
return@flatMapSingle ReactiveNetwork.checkInternetConnectivity()
}
Single.fromCallable { false }
}
.firstOrError() <-- fixed this line
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
If this issue occur again. I'll let you know.
Thanks.
from reactivenetwork.
Hi @pwittchen
I fixed above, so this crash count was decreased.
But, still was occurred same crash in Nougat os.
08-06 12:27:58.221 5830 5830 E MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
08-06 12:27:58.250 5830 5830 E MessageQueue-JNI: java.lang.IllegalArgumentException: Too many NetworkRequests filed
08-06 12:27:58.250 5830 5830 E MessageQueue-JNI: at android.os.Parcel.readException(Parcel.java:1688)
08-06 12:27:58.250 5830 5830 E MessageQueue-JNI: at android.os.Parcel.readException(Parcel.java:1637)
08-06 12:27:58.250 5830 5830 E MessageQueue-JNI: at android.net.IConnectivityManager$Stub$Proxy.listenForNetwork(IConnectivityManager.java:2400)
08-06 12:27:58.250 5830 5830 E MessageQueue-JNI: at android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:2856)
08-06 12:27:58.250 5830 5830 E MessageQueue-JNI: at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:3075)
08-06 12:27:58.250 5830 5830 E MessageQueue-JNI: at com.github.pwittchen.reactivenetwork.library.rx2.network.observing.strategy.MarshmallowNetworkObservingStrategy.observeNetworkConnectivity(MarshmallowNetworkObservingStrategy.java:80)
So, If possible to handle that properly within the library to next version, please let me know.
Thanks.
from reactivenetwork.
@pwittchen - I'm running into a similar issue - adding the logs from the library if that helps.
Version - 0.13.0
8-19-2021 12:05:37.012-0400 (thread main 2) E | NoOpCrashReporter: Uncaught exception on thread main (2)
android.net.ConnectivityManager$TooManyRequestsException
at android.net.ConnectivityManager.convertServiceException(ConnectivityManager.java:3977)
at android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:4205)
at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:4594)
at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:4564)
at com.github.pwittchen.reactivenetwork.library.network.observing.strategy.MarshmallowNetworkObservingStrategy.observeNetworkConnectivity(MarshmallowNetworkObservingStrategy.java:59)
at com.github.pwittchen.reactivenetwork.library.ReactiveNetwork.observeNetworkConnectivity(ReactiveNetwork.java:97)
at com.github.pwittchen.reactivenetwork.library.ReactiveNetwork.observeNetworkConnectivity(ReactiveNetwork.java:77)
from reactivenetwork.
Running into the same issue an Android wit the following SDK versions:
minSdkVersion 23
targetSdkVersion 28
Sadly no way to figure out in which scenario this happens.
from reactivenetwork.
Related Issues (20)
- "NetworkCallback was already unregistered" if re-using Observable HOT 4
- Migrate CI from Travis to GitHub Actions
- connection speed HOT 5
- fix error prone config for the RxJava2.x branch after bumping its version HOT 1
- Release 4.0.0-rx3 HOT 11
- [Question] What is a reason to use http instead of https protocol in your library internal requests? HOT 1
- How know which connection type is better ? HOT 1
- NetworkInfo.State Deprecated HOT 1
- Rx Java 3 support
- BroadCast Receiver stopped working HOT 5
- Google pixel wifi change event is received in 30 sec delay HOT 5
- MarshmallowNetworkObservingStrategy.java line 80 HOT 4
- isConnectedToInternet always false
- Internet connectivity issue: Could not establish connection with WalledGardenStrategy HOT 1
- ReactiveNetwork(RN) usage of IPv6 and DNS results prioritization HOT 1
- Will there be a version for Kotlin Flow ? HOT 2
- Fix Robolectric tests HOT 1
- How to check Internet Connection in Background continuously even if App is Running or Not/Closed.. HOT 1
- Th
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactivenetwork.