Wait for multiple Alamofire Requests to finish - ios

I have a below function in my network class which do the Alamofire Request process. I'm calling this function in my model class and when Alamofire finishes the request it calls delegate function which notify model class. My problem is that I'm calling this Alamofire function multiple times and both should finish before notifying model class. Now I handle it with very dummy way. As I searched, DispatchGroup can be used but I can't figure it out, how to implement it. Thank you.
Model Class
#objc func refresh_fiks(){
let network = Network()
network.delegate = self
self.teams = [[]]
network.getRequest(req: 1)
network.getRequest(req: 2)
}
Request Function:
func response(){
print(response_json.count)
if(response_json.count == path){
self.delegate?.sendJson(response_json)
}
}
func getRequest(req: Int) {
path = req
let rot = Router(method: .get, path: req, parameters: nil)
Alamofire.request(rot)
.response { response in
print(response.request?.url! as Any)
// check for errors
guard response.error == nil else {
// got an error in getting the data, need to handle it
print(response.error!)
let errorJson: JSON = [ "Error" : "Can't get the data!"]
self.response_json.append(errorJson)
self.response()
return
}
// make sure we got some JSON since that's what we expect
guard (response.data?.base64EncodedString()) != nil else {
print("Error: \(String(describing: response.error))")
let errorJson: JSON = [ "Error" : "Can't get the data!"]
self.response_json.append(errorJson)
self.response()
return
}
guard response.response?.statusCode == 200 else{
let errorJson: JSON = [ "Error" : "Can't get the data!"]
self.response_json.append(errorJson)
self.response()
return
}
let json = JSON(data: response.data!)
// get and print the title
if json != nil{
self.response_json.append(json)
self.response()
} else {
let errorJson: JSON = [ "Error" : "Can't get the data!"]
self.response_json.append(errorJson)
self.response()
return
}
}
}

I've made few changes in your code:
Network class:
func response(array: [JSON]){
print(array.count)
if(array.count == path){
self.delegate?.sendJson(array)
}
}
func getMultipleRequests(_ requests: [Int]) {
DispatchQueue.global(qos: .background).async {
let group = DispatchGroup()
var array: [JSON] = []
for request in requests {
group.enter()
self.getRequest(req: request, completion: { (json) in
array.append(json)
group.leave()
})
}
group.wait()
//this line below won't be called until all entries will leave the group
self.response(array: array)
}
}
func getRequest(req: Int, completion: #escaping (_ json: JSON) -> Void) {
path = req
let rot = Router(method: .get, path: req, parameters: nil)
Alamofire.request(rot)
.response { response in
print(response.request?.url! as Any)
// check for errors
guard response.error == nil else {
// got an error in getting the data, need to handle it
print(response.error!)
let errorJson: JSON = [ "Error" : "Can't get the data!"]
completion(errorJson)
return
}
// make sure we got some JSON since that's what we expect
guard (response.data?.base64EncodedString()) != nil else {
print("Error: \(String(describing: response.error))")
let errorJson: JSON = [ "Error" : "Can't get the data!"]
completion(errorJson)
return
}
guard response.response?.statusCode == 200 else{
let errorJson: JSON = [ "Error" : "Can't get the data!"]
completion(errorJson)
return
}
let json = JSON(data: response.data!)
// get and print the title
if json != nil{
completion(json)
} else {
let errorJson: JSON = [ "Error" : "Can't get the data!"]
completion(errorJson)
return
}
}
}
So the getRequest function now have a completion block that will return a json result off each request and the function getMultipleRequests that will receive a bunch of requests from anyone
This how you can use it
Your class, that calls refresh_fiks:
#objc func refresh_fiks(){
let network = Network()
network.delegate = self
self.teams = [[]]
network.getMultipleRequests([1,2])
}
Also, instead of using group.wait() you might need to use group.notify, it's better to notify that all entries leaved the group in specified queue, like the main:
group.notify(queue: DispatchQueue.main, execute: {
print("All Done")
self.response(array: array)
})
What to read about the DispatchGroups:
RayWenderlich
ALL ABOUT SWIFT

Related

Why is the firebase security rules not working?

I'm using RealTime DataBase for my application, it has security rules:
{
"rules": {
".read": "auth.uid != null",
".write": "auth.uid != null"
}
}
This is the code snippet where the data is getting:
func firstLaunchCataloguePartsFetchData(
result : #escaping((Result<[CatalogueParts], Error>) -> Void)
) {
if ConnectionManager.shared.isConnected {
let baseUrl = Constants.baseUrl + DirectoryType.catalogueParts.addJsonAbbreviation
guard let url = URL(string: baseUrl ) else { return }
URLSession.shared.dataTask(with: url) {data, response, error in
if let error = error {
result(.failure(error))
} else if let data = data,
let jsonString = String(data: data, encoding: .utf8),
let downloadedMarks = Mapper<CatalogueParts>().mapArray(JSONString: jsonString) {
result(.success(downloadedMarks))
} else {
result(.success([]))
}
}
.resume()
} else {
let error = NSError(domain: "Connection error detected", code: 911, userInfo: nil)
result(.failure(error))
}
}
Here I am using the received data to draw a table view:
func getFirstLaunchCatalogueParts() {
ShopManager.shared.firstLaunchCataloguePartsFetchData { [weak self] result in
guard let self = self else { return }
switch result {
case .success(let catalogueParts):
self.catalogueParts = catalogueParts
DispatchQueue.main.async {
self.tableView.reloadData()
}
case .failure(let error as NSError):
self.showErrorAlert(error: error, withAction: nil)
}
}
}
Even if I'm authorized, passed phone authorization (Auth.auth().currentUser?.uid has somevalue) - i can't display the tableView, received data is nil
But!
This works great, the table is rendered, everything works if the rules are made like this:
{
"rules": {
".read": "true",
".write": "auth.uid != null"
}
}
In my opinion the problem was this:
I was accessing Firebase using this method, it is not the basic method of reading firebase service data
let baseUrl = Constants.baseUrl + ".JSON"
// where baseUrl is just http path to my database and if you add ".JSON" to it - you
// open the contents of the database in Json
guard let url = URL(string: baseUrl ) else { return }
URLSession.shared.dataTask(with: url) {data, response, error in
Then I used the given Data to parse through the model:
let jsonString = String(data: data, encoding: .utf8),
let downloadedDataAsModel = Mapper<CatalogueParts>().mapArray(JSONString: jsonString)
The problem is that using this method, regardless of the rules - you get the data in any case, while the ObjectMapper processes and creates an instance, but it is incomplete, and no error occurs here
let downloadedDataAsModel = Mapper<CatalogueParts>().mapArray (JSONString: jsonString)
Therefore, this method does not allow rule settings other than "read : true"
I tried using this method and everything really worked - depending on the authorized status, access was allowed or restricted. Now it remains to decide how to use this method to get data in the form of Json for further processing =)
func fetchData(){
let ref = Constants.ref
ref.child("CatalogueParts").getData { error, snapshot in
guard error == nil else {
print(error!.localizedDescription)
return;
}
let someValue = snapshot.value
print(someValue)
}
}

how to wait for the URLSession to finish before returning the result from a function in Swift 3

Hi i have a beginner question, where i cant find a good solution for in Swift 3. I hope someone is able to hlep.
i have the following code that will check a rest api if the user credentials are valid or not. I want it to wait for the resquest is finished and then return true or false. now it is being send async.
Also any improvement in the way i check the JSON value's would be welcome too.
func CheckUsernamePassword(username :String ,code:String )-> bool {
var validCredentials = false
let urlString = "\(self.baseurl)/accounts/validateusernamepassword.json?username=\(username)&password=\(code)&api_key=\(self.api_key)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with:url!) { (data, response, error) in
if error != nil {
print("Error URLSession : \(error!)")
validCredentials = false
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
if parsedData["validated"] != nil {
if "\(parsedData["validated"]!)" == "1" {
print("Login credentials are correct")
validCredentials = true
}else {
print("Login credentials are not correct")
print("\(parsedData["validated"]!)")
print("\(parsedData["message"]!)")
validCredentials = false
}
}else{
print("Json Parse error: \(parsedData)")
validCredentials = false
}
} catch let error as NSError {
print("Error Parsing Json \(error)" )
validCredentials = false
}
}
}.resume()
return validCredentials
}
You cannot return something from an asynchronous task as a return value.
Do not wait, use a completion handler:
Replace the signature of the method (the name is supposed to start with a lowercase letter) with
func checkUsernamePassword(username: String, code: String, completion: #escaping (Bool)->() ) {
Delete the lines var validCredentials = false and return validCredentials
Replace all occurrences of validCredentials = false with completion(false) and
validCredentials = true with completion(true).
Call the method
checkUsernamePassword(username: "Foo", code: "Baz") { isValid in
print(isValid)
// do something with the returned Bool
DispatchQueue.main.async {
// update UI
}
}

Results won't append from JSON

The JSON file I am using: https://api.myjson.com/bins/49jw2
I am using SwiftyJSON for parsing.
The variable chores wont be populated outside the method parseJson
var chores: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tfWhat.delegate = self
tfHowMuch.delegate = self
loadJson()
// wont even print
for chore in self.chores {
print("beschrijving: " + chore)
}
// prints 0
print(chores.count)
}
func loadJson() -> Void {
let url = NSURL(string: "https://api.myjson.com/bins/49jw2")
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
} else {
if let data = data {
let json = JSON(data: data)
self.parseJson(json["appdata"]["klusjes"][])
} else {
print("no data")
}
}
}).resume()
}
func parseJson(jsonObject : JSON) -> Void {
for (_, value) in jsonObject {
self.chores.append(value["beschrijving"].stringValue)
}
// prints:
// beschrijving: Heg knippen bij de overburen
// beschrijving: Auto van papa wassen
for chore in self.chores {
print("beschrijving: " + chore)
}
// prints:
// 2
print(chores.count)
}
When you call an asynchronous method like NSURLSession.sharedSession().dataTaskWithURL it gets executed in the background, so whatever you launch just after this instruction is actually executed while the background task is running, so your array is not populated yet when you look at it.
A simple way to overcome this mistake is to use a "callback": a closure that will be executed once the data is available.
For example, let's add a callback
(json: JSON)->()
to the loadJson method:
func loadJson(completion: (json: JSON)->())
and place the call where the data will be available:
func loadJson(completion: (json: JSON)->()) {
let url = NSURL(string: "https://api.myjson.com/bins/49jw2")
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
} else {
if let data = data {
// the callback executes *when the data is ready*
completion(json: JSON(data: data))
} else {
print("no data")
}
}
}).resume()
}
and use it with a trailing closure like this:
loadJson { (json) in
// populate your array, do stuff with "json" here, this is the result of the callback
}

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