Swift 4 - Call nests with PromiseKit - ios

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)
}

Related

Alamofire Appending Data

I have a simple Alamofire request and i'm parsing it with SwiftyJSON. Here is code:
var fetchedData: [TestDB]? = []
func fetchData() {
Alamofire.request(url!, method: .get).validate().responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
self.fetchedData = [TestDB]()
for dict in json["articles"].array! {
let data = TestDB()
if let image = dict["urlToImage"].string {
data.imageUrls = image
}
if let titlee = dict["title"].string {
data.titlee = titlee
}
if let desc = dict["description"].string {
data.desc = desc
}
self.fetchedData?.append(data)
// If i print fetchedData here, i can see it has right values.
// print(fetchedData)
}
case .failure(let error):
print(error)
}
}
// If i try to print fetchedData here, it's empty.
}
As i said in code, i can't append and use my datas. I think that's something with Alamofire being asynchronous. But couldn't figure out why. Any help is appreciated. Thanks!
When you use asynchronous method you have some way to proceed, for example:
use callback
use RAC/observation
Callback:
func fetchData(completion: (result: [TestDB]) -> ) {
Alamofire.request(url!, method: .get).validate().responseJSON { response in
self.fetchedData = [TestDB]()
// fill self.fetchedData then:
completion(self.fetchedData)
}
})
Where you call fetchData:
self.fetchData(completion: {
// update collectionview?
self.collectionView.reloadData()
})
RAC:
You can find all documentation here.
Plus:
Some suggestion:
self.fetchedData = TestDB is not necessary, probably is sufficient self.fetchData.removeAll()
use ObjectMappar to map any json response to object

Returning a Promise from PromiseKit/Alamofire

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.

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 statement returns before information is received

I am having a little trouble with my swift code. The ending return statement runs before the the JSON value is stored so it keeps giving me nil. How can i do the return after the value been received?
func getArticleInfo(Id: String) -> String {
let url = val1 + val2 + val3
Alamofire.request(.GET, url).responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
let json = JSON(value)
let singleAsset = json["url"].string
}
}
case .Failure(let error):
print(error)
}
}
return singleAsset
}
Thanks for the help the other problem I’m having. SEE BELOW
I am trying to get the categories to populate with all the information then call the vc.displayCatName() after its done. But it does it late and i have to refresh the page before i can see the information.
Above that is just me assigning the JSON values to the keys that populate categories BELOW. But the vc.displayCatName() is a function from another view controller but it gets run before the category values are populated. So the only way i see the values is if i refresh the page manually using the Pull to Refresh. So i want the information to be populated then vc.displayCatName() should run
self.getAsset(id!) { (result) -> Void in
print("this is result \(result)")
let categories = Categories (categoryName: catName, imageId: id, catIdNumber: catIdNumber, imageUrl: result)
vc.cats.append(categories)
}
}
}
dispatch_async(dispatch_get_main_queue()) {
vc.displayCatName()
}
}
The reason for this is because the call that you are making is asynchronous in nature. Instead consider using a completion handler.
func getArticleInfo(Id: String, success: (String) -> Void ) {
let url = "www.Test.com"
Alamofire.request(.GET, url).responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
let singleAsset = json["url"].string
success(singleAsset!)
}
case .Failure(let error):
print(error)
success("TEST")
}
}
}
To Call:
getArticleInfo("test") { (asset) -> Void in
print(asset)
}

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