As title. I thought I've got it to work but it only appended 3 items into my array of NSMutableDictionary. I think maybe it's because the performsegue got called before the items are completely appended? How should I fix this problem?
(Also I dont know why but when I print(someDict.count) it gave me 17... it should only be 5 though since there are only 5 businesses)
Here's the code:
var searchBusinessDictionary: [NSMutableDictionary] = []
func retrieveBusinessFromAPI(id: String){
let url = URL(string: "https://api.yelp.com/v3/businesses/\(id)")
var request: URLRequest = URLRequest(url: url!)
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
let businessData = JSON(data)
var someDict = NSMutableDictionary()
someDict.setValue(businessData["name"], forKey: "Business Name")
someDict.setValue(businessData["url"], forKey: "Business Yelp URL")
someDict.setValue(businessData["image_url"], forKey: "Business image URL")
someDict.setValue(businessData["hours"][0]["is_open_now"], forKey: "Business Is Open Now")
someDict.setValue(businessData["price"], forKey: "Business Price")
someDict.setValue(businessData["rating"], forKey: "Business Rating")
someDict.setValue(businessData["location"]["display_address"], forKey: "Business Location")
self.searchBusinessDictionary.append(someDict)
self.performSegue(withIdentifier: "businessListSegue", sender: self)
})
task.resume()
}
Tried dispatching my performsegue to my main thread however I dont think the perform segue is called since my view didn't change
Here's the code for that:
@IBAction func saerch(_ sender: UIButton) {
self.yelpAPIRequest(completion: {
OperationQueue.main.addOperation {
self.performSegue(withIdentifier: "businessListSegue", sender: self)
}
})
}
func yelpAPIRequest(completion: ()->()){
let coordinate = YLPCoordinate(latitude: self.userLatitude, longitude: self.userLongitude)
let query = YLPQuery(coordinate: coordinate)
query.term = "dessert"
query.limit = 5
YLPClient.authorize(withAppId: clientId, secret: clientSecret).flatMap { client in
client.search(withQuery: query)
}.onSuccess { search in
if search.businesses.isEmpty == false {
let topBusiness = search.businesses
for i in topBusiness{
print(i.identifier)
self.retrieveBusinessFromAPI(id: i.identifier)
}
} else {
print("No businesses found")
}
}.onFailure { error in
print("Search errored: \(error)")
}
}