Returning a Promise from PromiseKit/Alamofire - ios

I'm trying to do this seemingly trivial thing:
static func list() -> Promise<[Activity]> {
let endpoint = "\(self.baseUrl)/v1/activities"
return Promise { fulfill, reject in
self.fetchHeaders { (headers) in
return Alamofire.request(
endpoint,
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: headers
).validate().responseJSON().then() { response in
guard let json = response as? JSON else {
reject(ActivityError.parse("Malformed JSON"))
}
guard let jsonActivities = json["activities"] as? [JSON] else {
reject(ActivityError.parse("Missing field"))
}
var activities: [Activity] = []
for jsonActivity in jsonActivities {
guard let activity = Activity(json: jsonActivity) else {
reject(ActivityError.parse("Unable to parse an Activity object"))
}
activities.append(activity)
}
fulfill(activities)
}.catch { error in
reject(ActivityError.network("HTTP response failure"))
}
}
}
}
However, the compiler (rightfully) complains that:
'guard' body may not fall through, consider using 'return' or 'break'
to exit the scope
I understand I need to return a Promise here. I just can't figure out what exactly to put below the reject() and fulfill() calls.

There's nothing wrong with reject or fulfill calls. The issue is that after you reject in your guard statement, you also have to return to exit the closure:
guard let json = response as? JSON else {
reject(ActivityError.parse("Malformed JSON"))
return
}
guard let jsonActivities = json["activities"] as? [JSON] else {
reject(ActivityError.parse("Missing field"))
return
}
The key point is that you do not want to conflate the promise that is returned by this method (which is later satisfied by fulfill or reject), with the fact that within this closure, you have to immediately exit the closure with a return in the guard clause.
I cannot reproduce this issue with your code (because you haven't provided a MCVE and there are references here that I cannot resolve). But here is a simplified rendition of your code illustrating the use of guard:
So, if not using PromiseKit/Alamofire, you can do:
func list() -> Promise<[String: Any]> {
return Promise { fulfill, reject in
Alamofire.request(url)
.validate()
.responseJSON { response in
switch response.result {
case .success(let json):
guard let dictionary = json as? [String: Any] else {
reject(ActivityError.malformed("not a dictionary"))
return
}
fulfill(dictionary)
case .failure(let error):
reject(error)
}
}
}
}
As you can see, you are returning a Promise, but inside the Alamofire closure, you simply are exiting your guard statement.
If you're using PromiseKit/Alamofire and call then, you presumably want to create a promise that it can return, such as:
func list() -> Promise<String> {
return Alamofire.request(endPoint)
.validate()
.responseJSON()
.then { value in
return Promise { fulfill, reject in
guard let dictionary = value as? [String: Any], let name = dictionary["name"] as? String else {
reject(ActivityError.malformed("not dictionary"))
return
}
fulfill(name)
}
}
}
Or if that's too hairy, you can pull out that parsing of the value:
func list() -> Promise<String> {
return Alamofire.request(endPoint)
.validate()
.responseJSON()
.then { value in
self.parse(value)
}
}
func parse(_ value: Any) -> Promise<String> {
return Promise { fulfill, reject in
guard let dictionary = value as? [String: Any], let name = dictionary["name"] as? String else {
reject(ActivityError.malformed("not dictionary"))
return
}
fulfill(name)
}
}
But, either way, even when using PromiseKit/Alamofire, you still just return within the guard clause.

Related

Swift 4 - Call nests with PromiseKit

Good morning everyone!
I'm doing an app with Swift 4.2 with Xcode10 and calls I manage with Alomofire together with PromiseKit.
At one point I need to load a screen at the beginning of the app with the terms of use, but only if they have updated it. And if the version has changed, that same call has the URL with the new endpoint. That endpoint contains the text to be displayed.Therefore, in that last case. We would have to make two calls.
Then I explain what I want to do:
To know if they have updated the version number I make a first call and check with the last one that I have saved in the device (NSUsserDefaults).The first call returns the following:
{ "legal_version": "1",
"legal_URL": "http://statics.....html" }
If it's the same version, I do not show the screen.
If the version has changed, I want to make a second call (with the URL that carries the answer of that first call "legal_URL")
he problem is that I do not know how to make that double call blocking. So do not load the main screen without checking the version number. And without knowing if I have to show or not the screen of legal terms again.
And all this is PromiseKit and nested calls.
Many thanks, for your help.
[Code updated]
let legalWarningRepository = LegalWarningRepository()
firstly {
legalWarningRepository.get(endpoint: "http://myserver.com/version")
}.then { json in
if let remoteVersion = json["version"] as? String, let
legalUrl = json["legal_URL"] as? String,
remoteVersion != localVersion {
return legalWarningRepository.get(endpoint: legalUrl)
}
}.done { json in
if json == nil {
// display main screen
}
else {
// display legal terms
}
}.catch { err in
print(err)
}
And inside "legalWarningViewController" I have the get method that you have passed me:
func get(endpoint: String) -> Promise<[String: Any]> {
return Promise { seal in
Alamofire.request(endpoint)
.validate()
.responseJSON { response in
switch response.result {
case .success(let json):
guard let json = json as? [String: Any] else {
return seal.reject(AFError.responseValidationFailed(reason:
.dataFileNil))
}
seal.fulfill(json)
case .failure(let error):
seal.reject(error)
}
}
}
}
On your first screen you can display an activity indicator while waiting for responses. When you have your responses you can display the appropriate view then.
In order to keep things simple you can create a generic method that "promisify" the Alamofire call:
func get(endpoint: String) -> Promise<[String: Any]> {
return Promise { seal in
Alamofire.request(endpoint)
.validate()
.responseJSON { response in
switch response.result {
case .success(let json):
guard let json = json as? [String: Any] else {
return seal.reject(AFError.responseValidationFailed(reason: .dataFileNil))
}
seal.fulfill(json)
case .failure(let error):
seal.reject(error)
}
}
}
}
Then in your the viewDidLoad or viewWillAppear of your home screen you can do this kind of logic:
firstly {
get(endpoint: "http://myserver.com/version")
}.then { json in
if let remoteVersion = json["version"] as? String, let legalUrl = json["legal_URL"] as? String, remoteVersion != localVersion {
return get(endpoint: legalUrl)
}
}.done { json in
if json == nil {
// display main screen
}
else {
// display legal terms
}
}.catch { err in
print(error.localizedDescription)
}
In your case a single call may be possible if you display the legal term in a WKWebView.
firstly {
get(endpoint: "http://myserver.com/version")
}.done { json in
if let remoteVersion = json["version"] as? String, let legalUrl = json["legal_URL"] as? String, remoteVersion != localVersion {
// display legal terms in a web view
}
// display main screen
}.catch { err in
print(error.localizedDescription)
}

Generic way to call Network Requests for API Client

I have provided the code for my firebase API client.
Is it smart to use generics, to initialize any entity straight from json this way?
To download lists, I needed an indicator to let me know that I'm requesting a list of entities- since there's different implementation, so I added a GETALL case to my HTTPMethod enum, is this bad, and something that would be confusing to others?
I also feel like this isn't flexible because I can't get desired entities on a node if there are nested at different levels. Hopefully that makes sense. So this probably doesn't follow open/closed principle, because If i have to nest my entities in firebase differently ill have to change implementation inside FirebaseAPI again.
From open source code i've seen, I haven't quite seen an rest client designed like this and not sure if I'm using an anti pattern or something. Any help or guidance to make this maintainable.
class FirebaseAPI {
private let session: URLSession
init() {
self.session = URLSession.shared
}
/// Responsible for Making actual API requests & Handling response
/// Returns an observable object that conforms to JSONable protocol.
/// Entities that confrom to JSONable just means they can be initialized with json.
func rx_fireRequest<Entity: JSONable>(_ endpoint: Endpoint) -> Observable<[Entity]> {
return Observable.create { [weak self] observer in
self?.session.dataTask(with: endpoint.request, completionHandler: { (data, response, error) in
/// Parse response from request.
let parsedResponse = Parser.init(data: data, response: response, error: error)
.parse()
switch parsedResponse {
case .error(let error):
observer.onError(error)
return
case .success(let data):
var entities = [Entity]()
/// Consider edge case where a list of entities are retrieved, rather than a single entity.
/// Iterate through each entity and initialize.
/// Denoted by 'GETALL' method.
switch endpoint.method {
case .GETALL:
/// Key (underscored) is unique identifier for each entity, which is not needed here.
/// value is k/v pairs of entity attributes.
for (_, value) in data {
if let value = value as? [String: AnyObject], let entity = Entity(json: value) {
entities.append(entity)
} else {
observer.onError(NetworkError.initializationFailure)
return
}
observer.onNext(entities)
observer.onCompleted()
return
}
default:
if let entity = Entity(json: data) {
observer.onNext([entity])
observer.onCompleted()
} else {
observer.onError(NetworkError.initializationFailure)
}
}
}
})
return Disposables.create()
}
}
}
Enum below Contains all request that can be made to firebase.
Conforms to Endpoint protocol, so one of these enum members will be the input for FirebaseAPI request method.
PROBLEM: Seems redundant to have multiple cases for CRUD operations when the only thing that changes is entity involved in the request.
enum FirebaseRequest {
case saveUser(data: [String: AnyObject])
case findUser(id: String)
case removeUser(id: String)
case saveItem(data: [String: AnyObject])
case findItem(id: String)
case findItems
case removeItem(id: String)
case saveMessage(data: [String: AnyObject])
case findMessages(chatroomId: String)
case removeMessage(id: String)
}
extension FirebaseRequest: Endpoint {
var base: String {
return ""https://<APPNAME>.firebaseio.com/""
}
var path: String {
switch self {
case .saveUser(let data): return "\(Constant.users)/\(data[Constant.id])"
case .findUser(let id): return "\(Constant.users)/\(id)"
case .removeUser(let id): return "\(Constant.users)/\(id)"
case .saveItem(let data): return "\(Constant.items)/\(data[Constant.id])"
case .findItem(let id): return "\(Constant.items)/\(id)"
case .findItems: return "\(Constant.items)"
case .removeItem(let id): return "\(Constant.items)/\(id)"
case .saveMessage(let data): return "\(Constant.messages)/\(data[Constant.id])"
case .findMessages(let chatroomId): return "\(Constant.messages)/\(chatroomId)"
case .removeMessage(let id): return "\(Constant.messages)/\(id)"
/// This is still incomplete... Will have more request.
}
}
var method: Method {
/// URLRequest method is GET by default, so just consider PUT & DELETE methods.
switch self {
/// If saving, return PUT
/// If removing, return DELETE
default: return .GET
}
}
var body: [String : AnyObject]? {
/// If saving, get associated value from enum case, and return that.
return nil
}
}
Endpoint Protocol
protocol Endpoint {
var base: String { get }
var path: String { get }
var method: Method { get }
var body: [String: AnyObject]? { get }
// no params needed for firebase. auth token just goes in url.
}
extension Endpoint {
private var urlComponents: URLComponents? {
var components = URLComponents(string: base)
components?.path = path + "auth=\(AuthService.shared.authToken)" + ".json"
return components
}
var request: URLRequest {
var request = URLRequest(url: urlComponents!.url!)
request.httpMethod = self.method.description
if let body = body {
do {
let json = try JSONSerialization.data(withJSONObject: body, options: [])
request.httpBody = json
} catch let error {
// error!
print(error.localizedDescription)
}
}
return request
}
}
HTTP Methods
enum Method {
case GET
/// Indicates how JSON response should be parsed differently to abastract a list of entities
case GETALL
case PUT
case DELETE
}
extension Method: CustomStringConvertible {
var description: String {
switch self {
case .GET: return "GET"
case .GETALL: return "GET"
case .PUT: return "PUT"
case .DELETE: return "DELETE"
}
}
}
AuthService
class AuthService {
private static let _shared = AuthService()
static var shared: AuthService {
return _shared
}
private let disposeBag = DisposeBag()
var currentUserId: String {
return Auth.auth().currentUser!.uid
}
var authToken: AuthCredential {
return FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
}
func rx_login(viewController: UIViewController) {
/// Facebook login
rx_facebookLogin(viewController: viewController)
.asObservable()
.subscribe(onNext: { [weak self] (credentials: AuthCredential, userInfo: [String: Any]) in
/// Firebase Login
self?.rx_firebaseLogin(with: credentials)
.asObservable()
.subscribe(onNext: { [weak self] (uid) in
/// TODO: Save in firebase db..
}).addDisposableTo((self?.disposeBag)!)
}).addDisposableTo(disposeBag)
}
// - MARK: facebook login
private func rx_facebookLogin(viewController: UIViewController) -> Observable<(AuthCredential, [String: Any])> {
return Observable<(AuthCredential, [String: Any])>.create { observable in
let loginManager = FBSDKLoginManager()
loginManager.logIn(withReadPermissions: ["public_profile", "email"], from: viewController) { (result, error) in
guard error == nil else {
observable.onError(AuthError.custom(message: error!.localizedDescription))
print("debugger: error: \(error!.localizedDescription)")
return
}
guard let accessToken = FBSDKAccessToken.current() else {
observable.onError(AuthError.invalidAccesToken)
print("debugger: invalid access token")
return
}
/// Facebook credentials to login with firebase.
let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.tokenString)
/// Build request to get user facebook info.
guard let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name"], tokenString: accessToken.tokenString, version: nil, httpMethod: "GET") else {
observable.onError(AuthError.facebookGraphRequestFailed)
print("debugger: could not create request.")
return
}
/// - Perform Request
request.start { (connection, result, error) in
guard error == nil else {
observable.onError(AuthError.custom(message: error!.localizedDescription))
print("debugger: error: \(error!.localizedDescription)")
return
}
print("Debugger: profile results: \(result)")
/// TODO: GET CITY FOR LOCALITY
guard let result = result as? [String: AnyObject], let name = result[Constant.name] as? String else {
observable.onError(AuthError.invalidProfileData)
print("debugger: error converting profile results")
return
}
/// Includes data needed to proceed with firebase login process.
observable.onNext((credential, ["name": name]))
observable.onCompleted()
print("Debugger: Successfull login")
}
}
return Disposables.create()
}
}
private func rx_firebaseLogin(with credential: AuthCredential) -> Observable<String> {
return Observable<String>.create { observable in
Auth.auth().signIn(with: credential) { (user, error) in
guard error == nil else {
observable.onError(AuthError.custom(message: error!.localizedDescription))
print("error firelogin \(error!.localizedDescription)")
return
}
guard user != nil else {
observable.onError(AuthError.invalidFirebaseUser)
print("debugger: error with user..")
return
}
observable.onNext(user!.uid)
observable.onCompleted()
}
return Disposables.create()
}
}
}

Alamofire request coming up nil

I'm developing an iOS app which user WebServices and I find Alamofire just perfect for what I'm doing but I'm having a problem; the app asks the user to login which is an Alamofire call and does it just fine.
The problem is, it has to create a collection view based on the content of another Alamofire request but is always nil.
func getJSON(URLToRequest: String) -> JSON {
let comp:String = (prefs.valueForKey("COMPANY") as? String)!
let params = ["company":comp]
var json:JSON!
let request = Alamofire.request(.POST, URLToRequest, parameters: params).responseJSON {
response in
switch response.result {
case .Success:
if let value = response.result.value {
json = JSON(value)
}
default:
json = JSON("");
}
}
debugPrint(request.response)
return json;
}
The same codeblock works perfect for the Login but doesn't in this case BTW the debug Print always print nil
You're trying to access to request.response before it has been set, remember that Alamofire works asynchronously, so you have to return in your case the JSON using closures, but remember that Alamofire also returns an error, so I strongly recommend use the following code instead:
func getJSON(URLToRequest: String, completionHandler: (inner: () throws -> JSON?) -> ()) {
let comp:String = (prefs.valueForKey("COMPANY") as? String)!
let params = ["company":comp]
let request = Alamofire.request(.POST, URLToRequest, parameters: params).responseJSON {
response in
// JSON to return
var json : JSON?
switch response.result {
case .Success:
if let value = response.result.value {
json = JSON(value)
}
completionHandler(inner: { return json })
case .Failure(let error):
completionHandler(inner: { throw error })
}
}
The trick is that the getJSON function takes an additional closure called 'inner' of the type () throws -> JSON?. This closure will either provide the result of the computation, or it will throw. The closure itself is being constructed during the computation by one of two means:
In case of an error: inner: {throw error}
In case of success: inner: {return json}
And then you can call it like in this way:
self.getJSON("urlTORequest") { (inner: () throws -> JSON?) -> Void in
do {
let result = try inner()
} catch let error {
print(error)
}
}
I hope this help you.

Return Bool in Alamofire closure

I use Swift 2 and Xcode 7.1.
I have a function who connect my users, but it will connect at my database with HTTP. I use Alamofire for execute this request. I want to know, from a view controller if the user is connected.
I have my function connect in a class. And i test connection in a ViewController.
Like this :
class user {
// ...
func connectUser(username: String, password: String){
let urlHost = "http://localhost:8888/project350705/web/app_dev.php/API/connect/"
let parametersSymfonyG = [
username, password
]
let url = UrlConstruct(urlHost: urlHost).setSymfonyParam(parametersSymfonyG).getUrl()
//var userArray = [String:AnyObject]()
Alamofire.request(.GET, url)
.responseString { response in
if let JSON = response.result.value {
var result = self.convertStringToDictionary(JSON)!
if result["status"] as! String == "success"{
let userArray = result["user"] as! [String:AnyObject]
userConnect = self.saveUser(userArray)
} else{
print("ERROR-CONNECTION :\n Status :\(result["status"]!)\n Code :\(result["code"]!)")
}
return ""
}
}
}
// ...
}
class MyViewController: UIViewController {
// ...
#IBAction func connect(sender: AnyObject?) {
// CONNECTION
User.connectUser(self.username.text!, password: self.password.text!)
// CHECK
if userConnect != nil {
print("connected")
}else{
print("NotConnected")
}
}
// ...
}
First solution : Return
To do so would require that my function returns a Boolean.
Only I can not use return.
Alamofire.request(.GET, url)
.responseString { response in
if let JSON = response.result.value {
var result = self.convertStringToDictionary(JSON)!
if result["status"] as! String == "success"{
let userArray = result["user"] as! [String:AnyObject]
userConnect = self.saveUser(userArray)
} else{
print("ERROR-CONNECTION :\n Status :\(result["status"]!)\n Code :\(result["code"]!)")
}
return "" // Unexpected non-void return value in void function
}
}
Second solution :
I can also test if the user has been logged, but before testing, I must wait for the function have finished loading.
users.connectUser(self.username.text!, password: self.password.text!)
// after
if userConnect != nil {
print("connected")
}else{
print("NotConnected")
}
I would prefer return a boolean. It will facilitate the processing.
Do you have a solution ?
I would suggest employing a completion handler in your connectUser method:
func connectUser(username: String, password: String, completion: #escaping (Bool) -> Void) {
// build the URL
// now perform request
Alamofire.request(url)
.responseString { response in
if let json = response.result.value, let result = self.convertStringToDictionary(json) {
completion(result["status"] as? String == "success")
} else {
completion(false)
}
}
}
You can then call it using:
users.connectUser(username.text!, password: password.text!) { success in
if success {
print("successful")
} else {
print("not successful")
}
}
// But don't use `success` here yet, because the above runs asynchronously
BTW, if your server is really generating JSON, you might use responseJSON rather than responseString, further streamlining the code and eliminating the need for convertStringToDictionary:
func connectUser(username: String, password: String, completion: #escaping (Bool) -> Void) {
// build the URL
// now perform request
Alamofire.request(url)
.responseJSON { response in
if let dictionary = response.result.value as? [String: Any], let status = dictionary["status"] as? String {
completion(status == "success")
} else {
completion(false)
}
}
}
If you've written your own server code to authenticate the user, just make sure you set the right header (because responseJSON not only does the JSON parsing for you, but as part of its validation process, it makes sure that the header specifies JSON body; it's good practice to set the header, regardless). For example in PHP, before you echo the JSON, set the header like so:
header("Content-Type: application/json");
The completion handler of your Alamofire.request method is asynchronous and it doesn't have a return type specified in its signature. Thats why you see an error when you provide a return statement in your completion handler closure.
You will have to split your request and response processing to separate methods and call the response processing method instead of using return statement.
Alamofire.request(.GET, url).responseString { response in
if let JSON = response.result.value {
var result = self.convertStringToDictionary(JSON)!
if result["status"] as! String == "success"{
let userArray = result["user"] as! [String:AnyObject]
userConnect = self.saveUser(userArray)
processSuccessResponse() //Pass any parameter if needed
} else{
print("ERROR-CONNECTION :\n Status :\(result["status"]!)\n Code :\(result["code"]!)")
processFailureResponse() //Pass any parameter if needed
}
}
}
func processSuccessResponse() {
//Process code for success
}
func processFailureResponse() {
//Process code for failure
}
My preferred way of doing this is to call a function in the completion handler. You can also set a boolean flag in order to check if the user is connected at any given time.
func connectUser(username: String, password: String, ref: MyClass) {
Alamofire.request(.GET, url)
.responseString { response in
var userIsConnected = false
if let JSON = response.result.value {
var result = self.convertStringToDictionary(JSON)!
if result["status"] as! String == "success"{
let userArray = result["user"] as! [String:AnyObject]
userConnect = self.saveUser(userArray)
userIsConnected = true
} else {
print("ERROR-CONNECTION :\n Status :\(result["status"]!)\n Code :\(result["code"]!)")
}
} else {
print("Response result nil")
}
ref.finishedConnecting(userIsConnected)
}
}
}
class MyClass {
var userIsConnected = false
func startConnecting() {
connectUser(username, password: password, ref: self)
}
func finishedConnecting(success: Bool) {
userIsConnected = success
... post-connection code here
}
}

Chain multiple Alamofire requests

I'm looking for a good pattern with which I can chain multiple HTTP requests. I want to use Swift, and preferrably Alamofire.
Say, for example, I want to do the following:
Make a PUT request
Make a GET request
Reload table with data
It seems that the concept of promises may be a good fit for this. PromiseKit could be a good option if I could do something like this:
NSURLConnection.promise(
Alamofire.request(
Router.Put(url: "http://httbin.org/put")
)
).then { (request, response, data, error) in
Alamofire.request(
Router.Get(url: "http://httbin.org/get")
)
}.then { (request, response, data, error) in
// Process data
}.then { () -> () in
// Reload table
}
but that's not possible or at least I'm not aware of it.
How can I achieve this functionality without nesting multiple methods?
I'm new to iOS so maybe there's something more fundamental that I'm missing. What I've done in other frameworks such as Android is to perform these operations in a background process and make the requests synchronous. But Alamofire is inherently asynchronous, so that pattern is not an option.
Wrapping other asynchronous stuff in promises works like this:
func myThingy() -> Promise<AnyObject> {
return Promise{ fulfill, reject in
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"]).response { (_, _, data, error) in
if error == nil {
fulfill(data)
} else {
reject(error)
}
}
}
}
Edit: Nowadays, use: https://github.com/PromiseKit/Alamofire-
I wrote a class which handles a chain of request one by one.
I created a class RequestChain wich takes Alamofire.Request as 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 I use it like this
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 with error \(errorResult?.error) for request \(errorResult?.request)")
}
}
Importent is that you are telling the Manager to not execute the request immediately
let manager = Manager.sharedInstance
manager.startRequestsImmediately = false
Hope it will help someone else
Swift 3.0 Update
class RequestChain {
typealias CompletionHandler = (_ success:Bool, _ errorResult:ErrorResult?) -> Void
struct ErrorResult {
let request:DataRequest?
let error:Error?
}
fileprivate var requests:[DataRequest] = []
init(requests:[DataRequest]) {
self.requests = requests
}
func start(_ completionHandler:#escaping CompletionHandler) {
if let request = requests.first {
request.response(completionHandler: { (response:DefaultDataResponse) in
if let error = response.error {
completionHandler(false, ErrorResult(request: request, error: error))
return
}
self.requests.removeFirst()
self.start(completionHandler)
})
request.resume()
}else {
completionHandler(true, nil)
return
}
}
}
Usage Example Swift 3
/// set Alamofire default manager to start request immediatly to false
SessionManager.default.startRequestsImmediately = false
let firstRequest = Alamofire.request("https://httpbin.org/get")
let secondRequest = Alamofire.request("https://httpbin.org/get")
let chain = RequestChain(requests: [firstRequest, secondRequest])
chain.start { (done, error) in
}
You have multiple options.
Option 1 - Nesting Calls
func runTieredRequests() {
let putRequest = Alamofire.request(.PUT, "http://httpbin.org/put")
putRequest.response { putRequest, putResponse, putData, putError in
let getRequest = Alamofire.request(.GET, "http://httpbin.org/get")
getRequest.response { getRequest, getResponse, getData, getError in
// Process data
// Reload table
}
}
}
This is definitely the approach I would recommend. Nesting one call into another is very simple and is pretty easy to follow. It also keeps things simple.
Option 2 - Splitting into Multiple Methods
func runPutRequest() {
let putRequest = Alamofire.request(.PUT, "http://httpbin.org/put")
putRequest.response { [weak self] putRequest, putResponse, putData, putError in
if let strongSelf = self {
// Probably store some data
strongSelf.runGetRequest()
}
}
}
func runGetRequest() {
let getRequest = Alamofire.request(.GET, "http://httpbin.org/get")
getRequest.response { [weak self] getRequest, getResponse, getData, getError in
if let strongSelf = self {
// Probably store more data
strongSelf.processResponse()
}
}
}
func processResponse() {
// Process that data
}
func reloadData() {
// Reload that data
}
This option is less dense and splits things up into smaller chunks. Depending on your needs and the complexity of your response parsing, this may be a more readable approach.
Option 3 - PromiseKit and Alamofire
Alamofire can handle this pretty easily without having to pull in PromiseKit. If you really want to go this route, you can use the approach provided by #mxcl.
Here is another way to do this (Swift 3, Alamofire 4.x) using a DispatchGroup
import Alamofire
struct SequentialRequest {
static func fetchData() {
let authRequestGroup = DispatchGroup()
let requestGroup = DispatchGroup()
var results = [String: String]()
//First request - this would be the authentication request
authRequestGroup.enter()
Alamofire.request("http://httpbin.org/get").responseData { response in
print("DEBUG: FIRST Request")
results["FIRST"] = response.result.description
if response.result.isSuccess { //Authentication successful, you may use your own tests to confirm that authentication was successful
authRequestGroup.enter() //request for data behind authentication
Alamofire.request("http://httpbin.org/get").responseData { response in
print("DEBUG: SECOND Request")
results["SECOND"] = response.result.description
authRequestGroup.leave()
}
authRequestGroup.enter() //request for data behind authentication
Alamofire.request("http://httpbin.org/get").responseData { response in
print("DEBUG: THIRD Request")
results["THIRD"] = response.result.description
authRequestGroup.leave()
}
}
authRequestGroup.leave()
}
//This only gets executed once all the requests in the authRequestGroup are done (i.e. FIRST, SECOND AND THIRD requests)
authRequestGroup.notify(queue: DispatchQueue.main, execute: {
// Here you can perform additional request that depends on data fetched from the FIRST, SECOND or THIRD requests
requestGroup.enter()
Alamofire.request("http://httpbin.org/get").responseData { response in
print("DEBUG: FOURTH Request")
results["FOURTH"] = response.result.description
requestGroup.leave()
}
//Note: Any code placed here will be executed before the FORTH request completes! To execute code after the FOURTH request, we need the request requestGroup.notify like below
print("This gets executed before the FOURTH request completes")
//This only gets executed once all the requests in the requestGroup are done (i.e. FORTH request)
requestGroup.notify(queue: DispatchQueue.main, execute: {
//Here, you can update the UI, HUD and turn off the network activity indicator
for (request, result) in results {
print("\(request): \(result)")
}
print("DEBUG: all Done")
})
})
}
}
Details
Alamofire 4.7.2
PromiseKit 6.3.4
Xcode 9.4.1
Swift 4.1
Full Sample
NetworkService
import Foundation
import Alamofire
import PromiseKit
class NetworkService {
static fileprivate let queue = DispatchQueue(label: "requests.queue", qos: .utility)
fileprivate class func make(request: DataRequest) -> Promise <(json: [String: Any]?, error: Error?)> {
return Promise <(json: [String: Any]?, error: Error?)> { seal in
request.responseJSON(queue: queue) { response in
// print(response.request ?? "nil") // original URL request
// print(response.response ?? "nil") // HTTP URL response
// print(response.data ?? "nil") // server data
//print(response.result ?? "nil") // result of response serialization
switch response.result {
case .failure(let error):
DispatchQueue.main.async {
seal.fulfill((nil, error))
}
case .success(let data):
DispatchQueue.main.async {
seal.fulfill(((data as? [String: Any]) ?? [:], nil))
}
}
}
}
}
class func searchRequest(term: String) -> Promise<(json: [String: Any]?, error: Error?)>{
let request = Alamofire.request("https://itunes.apple.com/search?term=\(term.replacingOccurrences(of: " ", with: "+"))")
return make(request: request)
}
}
Main func
func run() {
_ = firstly {
return Promise<Void> { seal in
DispatchQueue.global(qos: .background).asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
print("1 task finished")
DispatchQueue.main.async {
seal.fulfill(Void())
}
}
}
}.then {
return NetworkService.searchRequest(term: "John").then { json, error -> Promise<Void> in
print("2 task finished")
//print(error ?? "nil")
//print(json ?? "nil")
return Promise { $0.fulfill(Void())}
}
}.then {_ -> Promise<Bool> in
print("Update UI")
return Promise { $0.fulfill(true)}
}.then { previousResult -> Promise<Void> in
print("previous result: \(previousResult)")
return Promise { $0.fulfill(Void())}
}
}
Result
You can use the when method in PromiseKit to attach/append as many calls you want.
Here's an example from PromiseKit docs:
firstly {
when(fulfilled: operation1(), operation2())
}.done { result1, result2 in
//…
}
It worked perfectly for me and it's a much cleaner solution.
Call itself infinitely and DEFINE END CONDITION.
urlring for API link and Dictionary for json
WE may construct the queue model or delegate
func getData(urlring : String , para : Dictionary<String, String>) {
if intCount > 0 {
Alamofire.request( urlring,method: .post, parameters: para , encoding: JSONEncoding.default, headers: nil) .validate()
.downloadProgress {_ in
}
.responseSwiftyJSON {
dataResponse in
switch dataResponse.result {
case .success(let json):
print(json)
let loginStatus : String = json["login_status"].stringValue
print(loginStatus)
if loginStatus == "Y" {
print("go this")
print("login success : int \(self.intCount)")
self.intCount-=1
self.getData(urlring: urlring , para : para)
}
case .failure(let err) :
print(err.localizedDescription)
}
}
}else{
//end condition workout
}
}

Resources