GithubHelp home page GithubHelp logo

Comments (7)

pwittchen avatar pwittchen commented on May 27, 2024 2

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:

https://github.com/pwittchen/ReactiveNetwork/blob/RxJava2.x/library/src/main/java/com/github/pwittchen/reactivenetwork/library/rx2/network/observing/strategy/MarshmallowNetworkObservingStrategy.java#L80

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.

ExpensiveBelly avatar ExpensiveBelly commented on May 27, 2024 1

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.

pwittchen avatar pwittchen commented on May 27, 2024

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.

jonesn123 avatar jonesn123 commented on May 27, 2024

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.

jonesn123 avatar jonesn123 commented on May 27, 2024

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.

rpadma avatar rpadma commented on May 27, 2024

@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.

Blafasel3 avatar Blafasel3 commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.