Reputation: 4931
Just started using PromiseKit 6 with Swift. I've checked some examples and read the documentation. I have a scenario that seems trivial but I can't get around it. I have the following scenario:
func addModel(_ model: Model, image: UIImage?, completion: @escaping (Error?) -> Void) {
var modelToSet = model
var id = ""
firstly {
serviceWrapper.setImage(image!)
}.map { path in
modelToSet.imagePath = path
}.then { [serviceWrapper]
serviceWrapper.setModel(modelToSet)
}.map { documentId in
id = documentId
}.then {
CLLocationManager.promise()
}.done { [serviceWrapper] location in
serviceWrapper.setLocation(GeoPoint(latitude: location.lat, longitude: location.long), id: id)
}.catch { error in
completion(error)
}
}
As you can see, in this pseudo code I am force unwrapping the image. Any ideas how I can start the chain with the setImage method only if there is an image passed? Otherwise start from setModel directly. Or probably just return empty string if there is no image? What am I missing here?
Upvotes: 0
Views: 635
Reputation: 4931
After asking the PromiseKit community, I got what I needed. So this is how you can go with solving it with a local function:
func addModel(_ model: Model, image: UIImage?, completion: @escaping (Error?) -> Void) {
var modelToSet = model
var id = ""
func start() -> Promise<Void> {
guard let image = image else {
return Promise()
}
return serviceWrapper.uploadImage(image).map { path in
dishToSet.imagePath = path
}
}
firstly {
start()
}.then { [serviceWrapper]
serviceWrapper.setModel(modelToSet)
}.map { documentId in
id = documentId
}.then {
CLLocationManager.promise()
}.done { [serviceWrapper] location in
serviceWrapper.setLocation(GeoPoint(latitude: location.lat, longitude: location.long), id: id)
}.catch { error in
completion(error)
}
}
Upvotes: 1
Reputation:
Any ideas how I can start the chain with the setImage method only if there is an image passed?
You can use a guard statement to check the image is nil or not. If it is nil, execute the code needed to return. If not, continue the flow with a warpped image.
func addModel(_ model: Model, image: UIImage?, completion: @escaping (Error?) -> Void) {
var modelToSet = model
var id = ""
guard let image = image else {
// insert code to return if needed
return
}
firstly {
serviceWrapper.setImage(image)
}.map { path in
modelToSet.imagePath = path
}.then { [serviceWrapper]
serviceWrapper.setModel(modelToSet)
}.map { documentId in
id = documentId
}.then {
CLLocationManager.promise()
}.done { [serviceWrapper] location in
serviceWrapper.setLocation(GeoPoint(latitude: location.lat, longitude: location.long), id: id)
}.catch { error in
completion(error)
}
}
Upvotes: 0