2

I have a form for a user to change his address. This form contains two checkboxes. The user can choose if he wants to save the form as billing address, shipping address or both. The both-part is giving me trouble. The PromiseKit documentation(Getting started) says about multiple asynchronous tasks:

With completion handlers reacting to multiple asycnhronous operations is either slow or hard. Slow means doing it serially:

[...]

Promises are easier:

firstly {
when(fulfilled: operation1(), operation2()) }.then { result1, result2 in
//… }

when takes promises, waits for them to resolve and returns a promise with the results.

And of course, if any of them fail the chain calls the next catch, like any promise chain.

In the FAQ part it gives another code example:

let p1 = promise.then {
   // branch A
}

let p2 = promise.then {
    // branch B
}

when(fulfilled: p1, p2).catch { error in
   //…
}

So I tried to handle my code as follows:

var p1 = Promise()
var p2 = Promise()
if self.checkboxBilling.isChecked && self.checkboxShipping.isChecked{
    p1 = DbRequests.updateAddresses(userId: UserDefaultsManager.userId, name: name, line1: line1, line2: line2, postCode: postCode, city: city, addressType: .billing)
    p2 = DbRequests.updateAddresses(userId: UserDefaultsManager.userId, name: name, line1: line1, line2: line2, postCode: postCode, city: city, addressType: .shipping)
} else{
    if self.checkboxBilling.isChecked{
        p1 = Promise{fullfil, reject in
            fullfil(())
        }
        p2 = DbRequests.updateAddresses(userId: UserDefaultsManager.userId, name: name, line1: line1, line2: line2, postCode: postCode, city: city, addressType: .billing)
    }
    if self.checkboxShipping.isChecked{
        p1 = Promise{fullfil, reject in
            fullfil(())
        }
        p2 = DbRequests.updateAddresses(userId: UserDefaultsManager.userId, name: name, line1: line1, line2: line2, postCode: postCode, city: city, addressType: .shipping)
    }
}
when(fulfilled: p1, p2).then{ _ -> Void in
    print("success")
}.catch { error in
    print(error)
}

If only one of the boxes is ticked, everything seems to work fine. When I tick both boxes, it only works sometimes, though. Sometimes only one of the addresses is updated.

What am I missing here?


EDIT 28 Nov 17:

Here is the DbRequests.updateAdresses(...) function:

static func updateAddresses(userId: Int, name: String, line1: String, line2: String, postCode: String, city: String, addressType: AddressIdentifier) -> Promise<Void>{
    return Promise{fulfill, reject in
        var typeNumber: Int
        switch addressType{
        case .billing:
            typeNumber = 1
        case .shipping:
            typeNumber = 2
        }
        let parameters: [String : Any] = ["user_id": userId, "name": name, "line1": line1, "line2": line2, "post_code": postCode, "city": city, "address_type_id": typeNumber]
        Alamofire.request(App.rootdir + "/nailapp/functions/addAddresses.php", method: .post, parameters: parameters).validate().responseData() { response in
            switch response.result {
            case .success:
                fulfill(())
            case .failure(let error):
                reject(error)
            }
        }
    }
}
Tobias Grunwald
  • 161
  • 1
  • 3
  • 14

0 Answers0