Search code examples
iosswiftcloudkitpromisekit

PromiseKit and CloudKit saving (using Swift)


I'm new to PromiseKit and I'm a bit confused to how to handle multiple promises. I'm using CloudKit and saving two records, and when they are both saved, I want to do something on completion and something else on error. I thought I should do the following, but Xcode is complaining so obviously I must have misunderstood:

let savePromise1 : PMKPromise = db.saveRecord(record1)
let savePromise2 : PMKPromise = db.saveRecord(record2)

PMKPromise.when([ savePromise1, savePromise2 ]).then() { results in
    // handle success or errors
}

The error I get is "Cannot convert the experssion's type '(($T8) -> ($T8) -> $T7) -> (($T8) -> ($T7) -> $T7' to type 'PMKPromise'

I don't really understand what the error means, but I was expecting "results" to be an array of (result, error) tuples.

How should I write my "when" statement instead?

Cheers

Nik


Solution

  • Use the Swift version of PromiseKit, it now has CloudKit support:

    when(db.save(record1), db.save(record2)).then { (record1, record2)->Void in
    
    }
    

    The Swift version also has a tuple-when so you don't have to sort through an array of results.