PromiseKit / CoreLocation

Promises for Swift & ObjC
http://promisekit.org
MIT License
30 stars 29 forks source link

Promise do not wait for satisfying locations #10

Closed QuentinArnault closed 6 years ago

QuentinArnault commented 6 years ago

Hi,

I'm not sure if it is a bug or the expected behavior. I create a CLLocationManager promise with a satisfying block. This promise fulfill event if no locations are validated by the satisfying block. I would have expected that the promise will fulfill only if there is at least one satisfying location.

In CLLocationManager+promise.swif we have

    @objc fileprivate func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let block = satisfyingBlock {
            let satisfiedLocations = locations.filter { block($0) == true }
            seal.fulfill(satisfiedLocations)
        } else {
            seal.fulfill(locations)
        }
    }

I can propose a Pull Requests with something like:

    @objc fileprivate func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let block = satisfyingBlock,
            let satisfiedLocations = locations.filter { block($0) == true },
            !satisfiedLocations.isEmpty {
            seal.fulfill(satisfiedLocations)
        } else {
            seal.fulfill(locations)
        }
    }

With this implementation, we are waiting for at least one satisfying location.

Regards, Quentin