Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I optimize chaining async requests in Swift

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()
like image 682
Oleg Gordiichuk Avatar asked Apr 13 '17 10:04

Oleg Gordiichuk


Video Answer


2 Answers

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)

}
like image 72
ankit Avatar answered Sep 20 '22 10:09

ankit


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.

like image 25
Jeffery Thomas Avatar answered Sep 22 '22 10:09

Jeffery Thomas