Invalid conversion of recover block, PromiseKit
You should return Promise to chain correctly, like this :
self.supplierController
.update(supplier: supplier)
.recover { error -> Promise<Supplier> in
let supplier: Supplier = supplier
guard (error as NSError).code == 405 else {
throw error
}
return .value(supplier)
}
.done { (_: Supplier) in
changeCompanyIdAndAppendMessage()
}
source: https://github.com/mxcl/PromiseKit/blob/master/Documentation/CommonPatterns.md
Unable to use then on ios swift promisekit
You have to return a promise
when you do .then
because until .done
you are not finished. I don't see any issue in saving realm data in .done
as your method has no return type.
Incase you still want to use .then
, it can be done this way.
Translator().translators().then({ data -> Promise<[Translator]> in
return Promise.value(data)
}).done({ data in
// Save to realm
}).catch({ error in
print(error.localizedDescription)
})
Using PromiseKit I want to make multiple API calls
So I managed to achieve the batch calls I wanted using PromiseKit and handling all errors together:
private func batchDeleteItem(with itemId: UInt) -> Promise<(UInt, AppError?)> {
return Promise { seal in
firstly {
AppConfigManager.shared.getApiUrl(path: Constants.Api.deleteItemRequestPath, itemId)
}.then { url in
ApiManager.shared.request(url, method: .delete)
}.done { _ in
seal.fulfill((itemId, nil))
}.catch { error in
guard let appError = error as? AppError else {
seal.reject(error)
return
}
seal.fulfill((itemId, appError))
}
}
}
I call this method in a map, and in order to collect the ids of items that couldn't be deleted, I treat the catch with fulfill as wel - rejecting only server side error (this is what I needed for my case) :
return Promise { seal in
let promises = ids.map { itemId in
batchDeleteNode(with: itemId)
}
when(fulfilled: promises).done { results in
seal.fulfill(results)
}.catch { error in
seal.reject(error)
}
}
And to have all the errors grouped and waited for I use when(fulfilled: promises).done {...}
I'm not saying it's perfect but seems to work very well, and I learned a lot in the process regarding PromiseKit.
PromiseKit Framework - Ambiguous reference to member 'then()'
Try this code below. The promise thing should work. Modify it upon your needs.
gnEndpoints?.paymentToken(for: gnCreditCard).then({ token -> () in
if let token = token as? GNPaymentToken {
print(token)
}
}, { (error) -> () in
// catch your error
})
Related Topics
Adding Constraints Programmatically in UIview with UItextview
Dynamically Set Properties from Dictionary<String, Any> in Swift
No Trailing Closures Support for Methods with Default Parameter Values
Secidentity + Force Cast Violation: Force Casts Should Be Avoided. (Force_Cast)
How Do Uniqueness Constraints as (Comma,Separated,Attributes) Work with Swift in Coredata
The "Funk" Sound When Hitting Escape Key in App
Why Is Swift Giving Me Inaccurate Floating Point Arithmetic Results
Skease Action, How to Use Float Changing Action Setter Block
Map Dictionary Keys to Add Values - Swift
[Bool]' to 'Nil' Always Returns True - Issue in Swiftui
#If Canimport(Coreimage) Not Working in Swift Package Manager
What Do Detached and Assigncurrentcontext Meaning
How to Save Text Field Value in UIcollectionviewcell
Flattened Objects in JSON File to Nested Object Structure in Swift
Why Is This Predicate Format Being Turned into '= Nil'
Swift: Forward Keystrokes to a Different Process
Passing Values Between Viewcontrollers Based on List Selection in Swift