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

Fixed gpsLocationWith() init with builder which does not work correctly (cannot configure get continuous GPS values) #306

Closed
LoopingLouieX opened this issue Nov 17, 2020 · 4 comments
Assignees
Labels
Milestone

Comments

@LoopingLouieX
Copy link

Hi,

I've looked through your demo app in version 5.0 and tried out my use case and it seems working.
But after I implement the suggested demo code from the documentation to receive continous gps data, it just does so after the first time.

I've added the following code to my viewDidLoad:

        SwiftLocation.gpsLocationWith {
            // configure everything about your request
            $0.subscription = .continous // continous updated until you stop it
            $0.accuracy = .city
            $0.activityType = .automotiveNavigation
            
        }.then { result in // you can attach one or more subscriptions via `then`.
            switch result {
            case .success(let newData):
                print("New location: \(newData)")
   
            case .failure(let error):
                print("An error has occurred: \(error.localizedDescription)")
            }
        }

Am I missing something? Maybe do I have to add something to AppDelegate?
How do I get continous updates on this?
Thanks!

@LoopingLouieX LoopingLouieX changed the title Read out continous GPS value Get continous GPS value Nov 17, 2020
@LoopingLouieX LoopingLouieX changed the title Get continous GPS value Get continous GPS values Nov 17, 2020
@malcommac
Copy link
Owner

malcommac commented Nov 19, 2020

Did your request removed after the first result? Sorry I didn’t understand.

@hamzaozturk
Copy link
Contributor

@malcommac

let newRequest = GPSLocationRequest()

When you first initialize Location request without options, the subscription option is set as single. Therefore, it sets evictionPolicy for continuous subscription, too.

self.options = locationOptions ?? GPSLocationOptions()

Therefore, the request is removed after first result.

+ Add new request: BD3882C6-8A6B-4862-944B-5EDC3638835F
CLLocationManager: {
  "accuracy" : "any",
  "activityType" : "other",
  "minDistance" : -1,
  "services" : "[continousLocation]"
}
Authorization is set to = whenInUse
Received new locations: [<+40.89405586,+29.19512444> +/- 4334.32m (speed 0.00 mps / course 0.00) @ 11/20/20, 10:56:43 AM GMT+03:00]
Request 'BD3882C6-8A6B-4862-944B-5EDC3638835F' will be removed due to eviction policy [onReceiveData[1], onError]
- Remove request: BD3882C6-8A6B-4862-944B-5EDC3638835F
CLLocationManager: {
  "accuracy" : "any",
  "activityType" : "other",
  "minDistance" : -1,
  "services" : "[]"
}

@LoopingLouieX
Copy link
Author

Did the following now and it seems to be working, but don't know if that's the correct way:
MainController.swift

private var serviceOptions = GPSLocationOptions()

private func createRequest() {
        serviceOptions.subscription = .continous
        serviceOptions.activityType = .automotiveNavigation
        serviceOptions.accuracy = .city
        let request = SwiftLocation.gpsLocationWith(serviceOptions)

        AppDelegate.attachSubscribersToGPS([request])
        NotificationCenter.default.addObserver(self, selector: #selector(gpsUpdate), name: NOTIFICATION_GPS_DATA, object: nil)
    }

    override func viewDidLoad(){
        super.viewDidLoad()
          createRequest()
    }

 @objc private func gpsUpdate() {
        print ("New Data received", LocationManager.shared.coordinate)
    }

AppDelegate:

public let NOTIFICATION_GPS_DATA = Notification.Name("NOTIFICATION_GPS_DATA")

    public static func attachSubscribersToGPS(_ requests: [GPSLocationRequest]) {
        for request in requests {
            request.then(queue: .main) { result in
                NotificationCenter.default.post(name: NOTIFICATION_GPS_DATA, object: result, userInfo: nil)
                
                switch result {
                case .success(let visit):
                    sendLocalPushNotification(title: "New GPS Location", subtitle: visit.description, object: result.description)
                case .failure(let error):
                    sendLocalPushNotification(title: "GPS Error", subtitle: error.localizedDescription, object: result.description)
                }
            }
        }
    }
    
    public static func sendLocalPushNotification(title: String, subtitle: String, object: Any? = nil, afterInterval: TimeInterval = 3) {
        let content = UNMutableNotificationContent()
        content.title = title
        content.subtitle = subtitle
        content.sound = UNNotificationSound.default
        
        if let object = object {
            content.userInfo = ["result": object]
        }
        
        // show this notification five seconds from now
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: afterInterval, repeats: false)
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request)
    }

Hope I didn't forgot something in the code, but with this code I'll get continous updates + Local Push Notifications with an GPX file.

@malcommac
Copy link
Owner

@malcommac

let newRequest = GPSLocationRequest()

When you first initialize Location request without options, the subscription option is set as single. Therefore, it sets evictionPolicy for continuous subscription, too.

self.options = locationOptions ?? GPSLocationOptions()

Therefore, the request is removed after first result.

+ Add new request: BD3882C6-8A6B-4862-944B-5EDC3638835F
CLLocationManager: {
  "accuracy" : "any",
  "activityType" : "other",
  "minDistance" : -1,
  "services" : "[continousLocation]"
}
Authorization is set to = whenInUse
Received new locations: [<+40.89405586,+29.19512444> +/- 4334.32m (speed 0.00 mps / course 0.00) @ 11/20/20, 10:56:43 AM GMT+03:00]
Request 'BD3882C6-8A6B-4862-944B-5EDC3638835F' will be removed due to eviction policy [onReceiveData[1], onError]
- Remove request: BD3882C6-8A6B-4862-944B-5EDC3638835F
CLLocationManager: {
  "accuracy" : "any",
  "activityType" : "other",
  "minDistance" : -1,
  "services" : "[]"
}

thank you for your report, I've just fixed it by changing how the builder is created and passed:

    @discardableResult
    public func gpsLocationWith(_ optionsBuilder: ((GPSLocationOptions) -> Void)) -> GPSLocationRequest {
        let options = GPSLocationOptions()
        optionsBuilder(options)
        return gpsLocationWith(options)
    }

@malcommac malcommac changed the title Get continous GPS values gpsLocationWith() with builder does not work correctly (cannot configure get continuous GPS values) Nov 28, 2020
@malcommac malcommac added the bug label Nov 28, 2020
@malcommac malcommac added this to the 5.0.1 milestone Nov 28, 2020
@malcommac malcommac self-assigned this Nov 28, 2020
@malcommac malcommac changed the title gpsLocationWith() with builder does not work correctly (cannot configure get continuous GPS values) Fixed gpsLocationWith() init with builder which does not work correctly (cannot configure get continuous GPS values) Nov 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants