I have code that do requests chaining like A->B->C and I am using URLSession
all requests are done in right order and with expected behavior. But i am wondering how I can optimize this chaining because it looks quite complex and not reusable. I am looking for the suggestion how I can do this chaining in more flexible way.
My code:
URLSession.shared.dataTask(with: URLRequest(url: URL(string: "first")!)){ data , res , err in
let second = URLRequest(url: URL(string: "second")!)
URLSession.shared.dataTask(with: second){ data , res , err in
let third = URLRequest(url: URL(string: "second")!)
URLSession.shared.dataTask(with:third){ data , res , err in
}.resume()
}.resume()
}.resume()
Actually you can use dependencies using OperationQueues like below:
func operationQueueWithBlockandCancel(){
let mainQueue = OperationQueue.main
let operationBlock1 = BlockOperation()
let operationBlock2 = BlockOperation()
let operationBlock3 = BlockOperation()
operationBlock1.addExecutionBlock {
//Any task
}
operationBlock2.addExecutionBlock {
//Any task
}
operationBlock3.addExecutionBlock {
//Any task
}
//Add dependency as required
operationBlock3.addDependency(operationBlock2)
operationBlock2.addDependency(operationBlock1)
opQueue.addOperations([operationBlock2,operationBlock1,operationBlock3,], waitUntilFinished: false)
}
As @Paulw11 suggested:
PromiseKit + PMKFoundation
import PromiseKit
import PMKFoundation
let session = URLSession.shared
firstly {
session.dataTask(with: URLRequest(url: URL(string: "first")!))
} .then { data in
session.dataTask(with: URLRequest(url: URL(string: "second")!))
} .then { data in
session.dataTask(with: URLRequest(url: URL(string: "third")!))
} .then { data -> () in
// The data here is the data fro the third URL
} .catch { error in
// Any error in any step can be handled here
}
With 1 (and only 1) retry, you can use recover
. recover
is like catch
except it's expected that the previous then
can be retried. However, this is not a loop and executes only once.
func retry(url: URL, on error: Error) -> Promise<Data> {
guard error == MyError.retryError else { throw error }
// Retry the task if a retry-able error occurred.
return session.dataTask(with: URLRequest(url: url))
}
let url1 = URL(string: "first")!
let url2 = URL(string: "second")!
let url3 = URL(string: "third")!
let session = URLSession.shared
firstly {
session.dataTask(with: URLRequest(url: url1))
} .then { data in
session.dataTask(with: URLRequest(url: url2))
} .recover { error in
retry(url: url2, on: error)
} .then { data in
session.dataTask(with: URLRequest(url: url3))
} .recover { error in
retry(url: url3, on: error)
} .then { data -> () in
// The data here is the data fro the third URL
} .catch { error in
// Any error in any step can be handled here
}
NOTE: to make this work without specifying return types and needing a return
statement, I need the then
and recover
to be 1 line exactly. So I create methods to do the processing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With