Reputation: 31
I would like to perform request in swift for google places api and after i will get the results i would like to store them in a tableview. I think i resolved this in an unorthodox way. I used dispatch_time for 6 seconds (that is the time the request needs to get all the data) but after i put them in a table view the table view has lag and is moving slow. I want to do this the right way . Does anyone know how ?
this is what i tried :
self.fetchPlacesNearCoordinate(self.locValue1, radius: 1000, name: "food")
showActivityIndicator()
let delay = 6 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) {
self.activities()
self.hideActivityIndicator()
}
where fetchPlacesNearCoordinate() is my function that makes the request for the google places api
EDIT 1
this is how it is implemented the fetchPlacesNearCoordinate() function:
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, name : String) { var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=(locValue1.latitude),(locValue1.longitude)&rankby=distance&types=food" urlString += "&key=(apiServerKey)" urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
var placesTask = session.dataTaskWithURL(NSURL(string: urlString)!)
if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
placesTask.cancel()
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
do{
//******************Here's the line that displays error
placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {
(data, response, error) -> Void in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
//var placesArray = [GooglePlace]()
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? NSDictionary {
if let results = json["results"] as? NSArray {
for rawPlace:AnyObject in results {
// print(rawPlace)
var placename = ""
if let name = rawPlace["name"] as? NSString {
placename = name as String
self.locuri.insert(placename, atIndex: self.ct)
//self.locuri[ct] = placename
}
if let strada = rawPlace["vicinity"] as? NSString {
self.strazi.insert(strada as String, atIndex: self.ct)
}
if let iconurl = rawPlace["icon"] as? NSString {
self.iconuri.insert(iconurl as String, atIndex: self.ct)
}
if let placeid = rawPlace["place_id"] as? NSString {
self.placeiduri.insert(placeid as String, atIndex: self.ct)
}
self.ct++
}
}
}
} catch {
//handle error
}
}
}
placesTask.resume()
}
here it is how and when i insert data to tableview
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if (tableView == self.tblNearby){
let cell2 = tableView.dequeueReusableCellWithIdentifier("mycell", forIndexPath: indexPath) as! MyCustomCellTableViewCell
cell2.myTitleLabel.text = locuri[indexPath.row]
cell2.StreetLabel.text = strazi[indexPath.row]
if (iconuri.count != 0){
if let url = NSURL(string: iconuri[indexPath.row]) {
if let data = NSData(contentsOfURL: url){
//cell2.myImage.contentMode = UIViewContentMode.ScaleAspectFit
cell2.myImage.image = UIImage(data: data)
}
}
}
Upvotes: 0
Views: 426
Reputation:
You should have a method that look like this
func loadPlaces() {
startActivityIndicator()
dataProvider.fetchPlacesNearCoordinate(coordinate, radius:searchRadius, types: searchedTypes) { places in
stopActivityIndicator()
//append places object to your data
self.tableView.reloadData()
}
}
However if the fetchPlacesNearCoordinate method does not dispatch the call of the clojure/completionBlock to the main queue then it should look like this
func loadPlaces() {
startActivityIndicator()
dataProvider.fetchPlacesNearCoordinate(coordinate, radius:searchRadius, types: searchedTypes) { places in
dispatch_async(dispatch_get_main_queue()) {
stopActivityIndicator()
//append places object to your data
self.tableView.reloadData()
}
}
}
Upvotes: 0