Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise.onSuccess called immediately

I'm using BrightFutures and when I run the following code, sequence().onComplete and sequence().onSuccess is called before the geoCoder completionHandler is finished. Can you help me please to get this running?

self.uploadContentSequence = [Future<Future<Void, NoError>, NoError>]();

for post in posts {
     self.uploadContentSequence.append(future(self.preparePostUpload(post)))
}

self.uploadContentSequence.sequence().onComplete { (_) -> Void in
    print("onComplete")
}.onSuccess { (_) -> Void in
    print("onSuccess")
}.onFailure { (_) -> Void in
    print("onFailure")
}

[...]

func preparePostUpload(post: Post) -> Future<Void, NoError> {
    let promise = Promise<Void, NoError>()

    [...]

    let postLocation = CLLocation(latitude: Double(post.lat!), longitude: Double(post.lng!))
    let geocoder = CLGeocoder();
    let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String
    post.country = countryCode
    geocoder.reverseGeocodeLocation(postLocation, completionHandler: { (placemarks, locError) -> Void in
        [...]
        promise.success()
    });

    return promise.future
}
like image 626
Alexander Scholz Avatar asked Jan 13 '16 08:01

Alexander Scholz


1 Answers

As phimage pointed out in this issue: https://github.com/Thomvis/BrightFutures/issues/111 I was wrapping the future with a completed future.

like image 172
Alexander Scholz Avatar answered Oct 22 '22 16:10

Alexander Scholz