swift - Alamofire number of requests one after another -
i have number of requests witch call 1 after without having nested spaghetti code.
i tried serial dispatch queue
let queue = dispatch_queue_create("label", dispatch_queue_serial) alamofire.request(router.countries).responsestring { (response:response<string, nserror>) in print(1) } alamofire.request(router.countries).responsestring { (response:response<string, nserror>) in print(2) } alamofire.request(router.countries).responsestring { (response:response<string, nserror>) in print(3) }
but unfortunately not work. output of can 1,3,2 or 3,1,2 or other combination.
what best approach output 1,2,3 1 after other.
ok ended writing own implementation.
i created class requestchain
wich takes alamofire.request
parameter
class requestchain { typealias completionhandler = (success:bool, errorresult:errorresult?) -> void struct errorresult { let request:request? let error:errortype? } private var requests:[request] = [] init(requests:[request]) { self.requests = requests } func start(completionhandler:completionhandler) { if let request = requests.first { request.response(completionhandler: { (_, _, _, error) in if error != nil { completionhandler(success: false, errorresult: errorresult(request: request, error: error)) return } self.requests.removefirst() self.start(completionhandler) }) request.resume() }else { completionhandler(success: true, errorresult: nil) return } } }
and use
let r1 = alamofire.request(router.countries).responsearray(keypath: "endpoints") { (response: response<[countrymodel],nserror>) in print("1") } let r2 = alamofire.request(router.countries).responsearray(keypath: "endpoints") { (response: response<[countrymodel],nserror>) in print("2") } let r3 = alamofire.request(router.countries).responsearray(keypath: "endpoints") { (response: response<[countrymodel],nserror>) in print("3") } let chain = requestchain(requests: [r1,r2,r3]) chain.start { (success, errorresult) in if success { print("all have been success") }else { print("failed error \(errorresult?.error) request \(errorresult?.request)") } }
importent telling manager not execute request
let manager = manager.sharedinstance manager.startrequestsimmediately = false
hope else
Comments
Post a Comment