Swift Alamofire making multiple requests followed by a table reload - swift

I use this piece of code but the reload table always seems to load directly after the alamofire request instead of filling the data first. The braces are put in the correct way but it still loads Step 3 before Step 2.
Output:
- The filter list is not empty filling data according to filters.
- step 1
- step 3
- step 2: found 35 houses for Haarlem
- step 2: found 100 houses for Amsterdam
println("The filter list is not empty filling data according to filters.")
if let castedFilters = filters as? [Filter] {
println("step 1")
for filter in castedFilters{
var parameters : [String : NSObject] = ["apisleutel": "#########", "module": "Objecten", "get": "Huur", "plaats": filter.plaats, "pt": filter.maximumprijs, "pv": filter.minimumprijs, "wov": filter.oppervlakte, "ka": filter.kamers, "output": "json"]
self.makeCall(parameters) { responseObject, error in
let json = JSON(responseObject!)
/**/
let count: Int? = json["Response"]["objecten"]["object"].array?.count
if((count) != nil)
{
println("step 2: found \(count!) houses for "+filter.plaats)
if let ct = count {
for index in 0...ct-1 {
//Adding house to houses array
var adres = json["Response"]["objecten"]["object"][index]["adres"].string
let newHouse = House(straat: adres!)
self.houses.append(newHouses)
}
}
}
else
{
let alert = UIAlertView()
alert.title = "Fout"
alert.message = "No houses found, consider changing the filters."
alert.addButtonWithTitle("Ok")
alert.show()
}
return
}
}
println("step 3")
tableView.reloadData()
}
Call function
func makeCall(parameters: [String : NSObject], completionHandler: (responseObject: NSDictionary?, error: NSError?) -> ()) {
var heap: NSDictionary
Alamofire.request(.GET, "http://www.huizenzoeker.nl/api/v2/", parameters: parameters)
.responseJSON { request, response, responseObject, error in
completionHandler(responseObject: responseObject as? NSDictionary, error: error)
}
}

Related

Swift 4 Get multiples places from Google Places API

I have trouble with fetching multiples places from Google Places API. The problem is... if i fetch only one pin type like Bars, its ok, no problem. But if i trying to get Restaurants, Bars, Casino... multiples types, it gives my only first place, in our case Restaurants.
I tried make same request with Postman with link below... but for example
restaurants 1030 lines of JSON
political 83 lines
trying to get restaurants + political = 965 lines of JSON.
I use this code to get places i want:
func fetchPlacesNearCoordinate(_ coordinate: CLLocationCoordinate2D, radius: Double, types:[String], completion: #escaping PlacesCompletion) -> Void {
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(50000)&rankby=prominence&sensor=true&key=\(googleApiKey)"
let typesString = types.count > 0 ? types.joined(separator: "|") : "food"
urlString += "&types=\(typesString)"
urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? urlString
guard let url = URL(string: urlString) else { completion([]); return }
if let task = placesTask, task.taskIdentifier > 0 && task.state == .running {
task.cancel()
}
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
placesTask = session.dataTask(with: url) { data, response, error in
if data != nil{
for el in data!{
//print(el)
}
}
var placesArray: [PlaceContent] = []
defer {
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
completion(placesArray)
}
}
guard let data = data else { return }
do{
let decode = try JSONDecoder().decode(GooglePlacesAnswer.self, from: data)
placesArray = (decode.results?.map{ $0.toPlaceContent() }) ?? []
} catch let value{
print(value.localizedDescription)
}
}
placesTask?.resume()
}
You can't get places for multiple types as stated in official documentation. You have to make multiple requests and combine the results.
https://developers.google.com/maps/documentation/javascript/places
type — Restricts the results to places matching the specified type.
Only one type may be specified (if more than one type is provided, all
types following the first entry are ignored). See the list of
supported types.

Can't get data returned from dataTask()

For one week I have been trying to get a string returned from dataTask().
I already read a lot here on StackOverFlow and also from serval sites where they tackle this topic. For example, this one. So I already understand that it's that the dataTask doesn't directly return values, cause it happens on different threads and so on. I also read about closures and completion handlers. I really got the feeling that I actually already got a little clue what this is about. But I can't get it to work.
So this is my code. I just post the whole code so no-one needs to worry that the problem sticks in a part which I don't show. Everything is working fine until I try to return a value and save it for example in a variable:
func requestOGD(code gtin: String, completion: #escaping (_ result: String) -> String) {
// MARK: Properties
var answerList: [String.SubSequence] = []
var answerDic: [String:String] = [:]
var product_name = String()
var producer = String()
// Set up the URL request
let ogdAPI = String("http://opengtindb.org/?ean=\(gtin)&cmd=query&queryid=400000000")
guard let url = URL(string: ogdAPI) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /todos/1")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result, which is String. It willbecome split and placed in a dictionary
do {
let answer = (String(decoding: responseData, as: UTF8.self))
answerList = answer.split(separator: "\n")
for entry in answerList {
let entry1 = entry.split(separator: "=")
if entry1.count > 1 {
let foo = String(entry1[0])
let bar = String(entry1[1])
answerDic[foo] = "\(bar)"
}
}
if answerDic["error"] == "0" {
product_name = answerDic["detailname"]!
producer = answerDic["vendor"]!
completion(product_name)
} else {
print("Error-Code der Seite lautet: \(String(describing: answerDic["error"]))")
return
}
}
}
task.resume()
Here I call my function, and no worries, I also tried to directly return it to the var foo, also doesn't work The value only exists within the closure:
// Configure the cell...
var foo:String = ""
requestOGD(code: listOfCodes[indexPath.row]) { (result: String) in
print(result)
foo = result
return result
}
print("Foo:", foo)
cell.textLabel?.text = self.listOfCodes[indexPath.row] + ""
return cell
}
So my problem is, I have the feeling, that I'm not able to get a value out of a http-request.
You used a completion handler in your call to requestOGD:
requestOGD(code: listOfCodes[indexPath.row]) {
(result: String) in
// result comes back here
}
But then you tried to capture and return that result:
foo = result
return result
So you're making the same mistake here that you tried to avoid making by having the completion handler in the first place. The call to that completion handler is itself asynchronous. So you face the same issue again. If you want to extract result at this point, you would need another completion handler.
To put it in simple terms, this is the order of operations:
requestOGD(code: listOfCodes[indexPath.row]) {
(result: String) in
foo = result // 2
}
print("Foo:", foo) // 1
You are printing foo before the asynchronous code runs and has a chance to set foo in the first place.
In the larger context: You cannot use any asynchronously gathered material in cellForRowAt. The cell is returned before the information is gathered. That's what asynchronous means. You can't work around that by piling on further levels of asynchronicity. You have to change your entire strategy.

Firestore: Reading multiple documents in transaction

When I run the Firestore transaction it gives me the error :
"Every document read in a transaction must also be written in that transaction."
But all the documents that I am trying to read in the transaction are also being written to in the code below ?
db.runTransaction({ (transaction, errorPointer) -> Any? in
let doc1: DocumentSnapshot
let doc2: DocumentSnapshot
let doc3: DocumentSnapshot
do {
try doc1 = transaction.getDocument(self.doc1Ref)
try doc2 = transaction.getDocument(self.doc2Ref)
try doc3 = transaction.getDocument(self.doc3Ref)
} catch let fetchError as NSError {
errorPointer?.pointee = fetchError
return nil
}
// WRITES TO DOC1, DOC2, DOC3
guard let userPostCount = doc1.data()?["postCount"] as? Int,
let locationPostCount = doc2.data()?["postCount"] as? Int,
let itemPostCount = doc3.data()?["postCount"] as? Int,
let locationAvgRating = doc2.data()?["averageRating"] as? Float,
let itemAvgRating = doc3.data()?["averageRating"] as? Float else { return nil }
// Create post on another document not in transaction
transaction.setData(post.dictionary, forDocument: self.doc4Ref)
// Update counts for #userPosts, #locationPosts, #itemPosts, avgLocationRating, avgItemRating
transaction.updateData(["postCount": userPostCount + 1], forDocument: self.doc1Ref)
let newAvgLocationRating = ((avgLocationRating * Float(locationPostCount)) + Float(post.rating)) / (Float(locationPostCount) + 1.0)
transaction.updateData(["postCount": locationPostCount + 1, "averageRating": newAvgLocationRating], forDocument: self.doc2Ref)
let newAvgItemRating = ((avgItemRating * Float(itemPostCount)) + Float(post.rating)) / (Float(itemPostCount) + 1.0)
transaction.updateData(["postCount": locationPostCount + 1, "averageRating": newAvgItemRating], forDocument: self.doc3Ref)
// Add postID to user and location
transaction.setData(["postID": self.postRef.documentID, "locationID": post.locationID, "itemID": post.itemID, "rating": post.rating, "timestamp": post.timestamp], forDocument: self.doc1Ref.collection("posts").document(self.postRef.documentID))
transaction.setData(["postID": self.postRef.documentID, "rating": post.rating, "timestamp": post.timestamp], forDocument: self.doc3Ref.collection("posts").document(self.postRef.documentID))
return nil
}) { (object, error) in
if let error = error {
print(error)
} else {
print("done")
}
}
Is it just not possible to do multiple .getDocument(documentReference) in a transaction?
Is there another way to accomplish this issue?
you should put all your if operations in do and else operations in catch
don't use them outside the block.
thats what the error is saying "Every document read in a transaction must also be written in that transaction.".
for ex. your code
// WRITES TO DOC1, DOC2, DOC3
guard let userPostCount = doc1.data()?["postCount"] as? Int,
let locationPostCount = doc2.data()?["postCount"] as? Int,
let itemPostCount = doc3.data()?["postCount"] as? Int,
let locationAvgRating = doc2.data()?["averageRating"] as? Float,
let itemAvgRating = doc3.data()?["averageRating"] as? Float else { return nil }
is doing operations with doc1,doc2 outside the scope of do catch block
I just noticed you are using Apple's swift scripting. My answer is standards based JavaScript but I hope it helps you. I don't use or know anything about Swift...
As far as reading data from 3 documents simultaneously, the way to do this is using javascript's .promise.all. While the below code is not precisely your solution, you can get the just of how to do it.
You basically push each .get into an array then you .promise.all that array .then you iterate over the returned data.
try {
targetRef.get().then(function(snap) {
if (snap.exists) {
for (var key in snap.data()) {
if (snap.data().hasOwnProperty(key)) {
fetchPromise = itemRef.doc(key).get();
fetchArray.push(fetchPromise);
}
}
Promise.all(fetchArray).then(function(values) {
populateSelectFromQueryValues(values,"order-panel-one-item-select");
if(!selectIsEmpty("order-panel-one-item-select")){
enableSelect("order-panel-one-item-select");
}
});
}
}).catch(function(error) {
toast("error check console");
console.log("Error getting document:", error);
});
}

True becomes false using SwiftyJson in swift

I am getting the opposite value of a boolean when using .boolValue
I have this code.
let _ = channel.bind(eventName: "like-unlike-cake", callback:
{(data:Any?)->Void in
let likedCake = JSON(data!)["like"]
print("liked cake: \(likedCake)")
print("Boolean: \(likedCake["liked_by_current_user"].boolValue)")
liked cake: {
"liked_by_current_user" : true
}
}
Boolean: false
But when I use .bool it gives me nil. Does someone help me on this.
UPDATE This would be the json I want to fetch
{
"like": {
"id": "66642122-7737-4eac-94d2-09c9a35cbef8",
"liked_by_current_user": true
}
}
Try the following:
let _ = channel.bind(eventName: "like-unlike-cake", callback {
(data:Any?)->Void in
if let jsonData = data {
let json = JSON(jsonData)
let isLikedByCurrentUser = json["like"]["liked_by_current_user"].boolValue
print(isLikedByCurrentUser)
} else {
// your data is nil
debugPrint("data error")
}
}
also you can try to cast your data: Any into Data by replacing the line:
if let jsonData = data as? Data {
let json = JSON(data: jsonData)...
cause sometimes SwiftyJSON have problems with creating JSON objects from Any.

Swift closure with Alamofire

I am making API calls to a server. I am leveraging Alamofire to handle this. I'm trying to create a function that uses Alamofire's GET function to return an object based on a custom class that holds the various outputs provided by this GET function.
It's not clear to me the way in which to do this.
Here's my custom class that will hold details about the response:
import Foundation
class ResponsePackage {
var success = false
var response: AnyObject? = nil
var error: NSError? = nil
}
In another class I have the following function:
func get(apiEndPoint: NSString) -> ResponsePackage {
let responsePackage = ResponsePackage()
Alamofire
.request(.GET, apiEndPoint)
.responseJSON {(request, response, JSON, error) in
responsePackage.response = JSON
responsePackage.success = true
responsePackage.error = error
}
return responsePackage
}
This returns nil as the call to the server is not complete before the return gets executed. I know that I should be able to do this with closures, but I am not sure how to construct this.
The code between the {} is the equivalent of block in objective-C : this is a chunk of code that gets executed asynchronously.
The error you made is where you put your return statement : when you launch your request, the code in {} is not executed until the framework received a response, so when the return statement is reached, chances are, there is still no response. You could simply move the line :
return responsePackage
inside the closure, so the func return only when it has received a response. This is a simple way, but it's not really optimal : your code will get stuck at waiting for the answers. The best way you can do this is by using closure, too. This would look something like :
func get(apiEndPoint: NSString, completion: (response: ResponsePackage) -> ()) -> Bool {
let responsePackage = ResponsePackage()
Alamofire
.request(.GET, apiEndPoint)
.responseJSON {(request, response, JSON, error) in
responsePackage.response = JSON
responsePackage.success = true
responsePackage.error = error
completion(response: responsePackage)
}
}
I make an example follow your question about responseJSON with closures:
Follow this little steps:
First of all you can create your custom types in a general class (for example a Constants.swift class):
typealias apiSuccess = (result: NSDictionary?) -> Void
typealias apiProgress = (result: NSDictionary?) -> Void // when you want to download or upload using Alamofire..
typealias apiFailure = (error: NSDictionary?) -> Void
Then in your class:
// Normal http request with JSON response..
func callJSONrequest(url:String, params:[String: AnyObject]?, success successBlock :apiSuccess,
failure failureBlock :apiFailure) {
Alamofire.request(.GET, url, parameters: params, encoding: ParameterEncoding.URL)
.responseJSON { response in
print("\(response.request?.URL)") // original URL request
//print(response.response) // URL response
//print(response.data) // server data
//print(response.result) // result of response serialization
if response.result.isSuccess {
let jsonDic = response.result.value as! NSDictionary
successBlock(result: jsonDic)
} else {
let httpError: NSError = response.result.error!
let statusCode = httpError.code
let error:NSDictionary = ["error" : httpError,"statusCode" : statusCode]
failureBlock(error: error)
}
}
}
}
func myCommonFunction() {
let myApiSuccess: apiSuccess = {(result: NSDictionary?) -> Void in
print ("Api Success : result is:\n \(result)")
// Here you can make whatever you want with result dictionary
}
let myApiFailure: apiFailure = {(error: NSDictionary?) -> Void in
print ("Api Failure : error is:\n \(error)")
// Here you can check the errors with error dictionary looking for http error type or http status code
}
var params :[String: AnyObject]?
let name : String! = "this is my name"
let id : String! = "000a"
params = ["name" : name, "id" : id]
let url : String! = "https://etc..."
callJSONrequest(url, params:params, success: myApiSuccess, failure: myApiFailure)
}