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

Observables does not send updated value when app enters foreground #24

Open
havard024 opened this issue Mar 5, 2019 · 6 comments
Open

Comments

@havard024
Copy link

Our app requires the user to allow access to the devices location.
To handle this we first check if the location service is enabled, if it's not enabled we present a view controller letting the user know that he / she needs to enable the location service to continue.
The user would then most likely put the app in the background, enable the location service, open the app again.

In the view we present to the user when location service is disabled, we would like to automatically detect if the location service has been enabled, the code below is our attempt to react to the isEnabled event.

viewDidLoad() {
  super.viewDidLoad()
  manager.rx
    .isEnabled
    .debug("isEnabled")
    .subscribe(onNext: { isEnabled in
      if isEnabled {
        DispatchQueue.main.async {
          self.dismiss(animated: true, completion: nil)
        }
      }
    })
    .disposed(by: bag)
}

When we test this by putting the app in the background then to foreground, no event is triggered.

Is this something the library is designed to handle?

If not, how would one go about creating this functionality in an app?

@bobgodwinx
Copy link
Member

bobgodwinx commented Mar 5, 2019

By the way this is the wrong way to do subscription. you should use subscribeOn(MainScheduler.instance) instead.
And you are also missing something before the call of manager.rx...

        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

please see the example project for more

@unxavi
Copy link

unxavi commented Apr 1, 2019

Hi,

Why do you need to startUpdatingLocation to know if location services are on? this is a static method of CLLocationManager and shouldn't be related between them.

Using the example I project config, I also see that sometimes it's not deliver in background when you go to settings and change locations services on/off

@havard024
Copy link
Author

@bobgodwinx I'm referring to the example on line 85

manager.rx
    .isEnabled
    .debug("isEnabled")
    .subscribe(onNext: { _ in })
    .disposed(by: bag)

@bobgodwinx
Copy link
Member

@havard024 I think I now understand what you're trying todo here. Correct me if I am wrong.

  1. You are putting the app in background and disactivating location services correct ?

@havard024
Copy link
Author

@bobgodwinx Yes exactly and when you put the app in foreground again, there is no event to notify that the location service is disabled

@bobgodwinx
Copy link
Member

@havard024 Here is my advice: The API is actually working as expected. The event you are expecting is not emitted because CLLocationManager actually emitted Error events which you could use for this purpose.

  • didError -> Event next((manager: <CLLocationManager: 0x600003acbfb0>, error: Error Domain=kCLErrorDomain Code=1 "(null)")) the code here is 1 which means kCLErrorDenied.
  • This error is actually indicating that the manager is locked out of CLLocationManager
  • would rather use another event here which is the didChangeAuthorization that also gets emitted when you try switching of the LocationServices
        manager.rx
            .didChangeAuthorization
            .debug("didChangeAuthorization")
            .subscribe(onNext: { e in
                let a = e.status
                switch a {
                case .denied, .notDetermined, .restricted:
                    print("Stay there blocking the screen! don't move")
                default:
                    print("Stop blocking the screen let him enter now")
                }
            })
            .disposed(by: bag)

Let me know if this helps.

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

3 participants