tbruyelle/RxPermissions

Twice receiving 'granted' value in onSubscribe

Opened this issue · 3 comments

Here are my code:

`    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is Activity) {
            rxPermissions = RxPermissions(context)
            fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
            settingsClient = LocationServices.getSettingsClient(context)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        logger.w("onViewCreated: ")
        if (checkPermissions()) {
            initMap(savedInstanceState)
        } else {
            requestPermissions(savedInstanceState)
        }
    }

    private fun requestPermissions(bundle: Bundle?) {
        rxPermissions
                ?.request(android.Manifest.permission.ACCESS_FINE_LOCATION)
                ?.subscribe({ granted: Boolean ->
                    logger.w("requestPermissions: granted - $granted")
                    if (granted) {

                        startLocationUpdates()
                        initMap(bundle)
                        
                    } else {
                        onError(ForceLeaveAppException(resources.getString(R.string.not_granted_permission_error_message)), null)
                    }
                }, { exception: Throwable? ->
                    logger.w("requestPermissions: exception - ${exception?.localizedMessage}")
                })
    }

    private fun checkPermissions(): Boolean {
        return rxPermissions!!.isGranted(android.Manifest.permission.ACCESS_FINE_LOCATION)
    }
`

The problem is that when I'm requesting permission ACCESS_FINE_LOCATION then I have got 2 (two) response in Subscriber and is shown 2 lines in logs
MapFragment: requestPermissions: granted - true

Is that normal behavior?

@zezekalo Please ensure onViewCreated is invoked only a single time.

If not that means you'll have to use an other method for requestPermissions.

https://github.com/tbruyelle/RxPermissions#important-read

Yes, I checked and onViewCreated is invoked one time.
I noticed that if I request two permissions together like
?.request(android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION)
so in that case I've got only one response in callback.

in that case I've got only one response in callback

Use requestEach to have one response per permission (please check the README)