我想在 swift 为google places api执行请求,之后我会得到结果我想将它们存储在tableview中。我想我以非正统的方式解决了这个问题。我使用dispatch_time 6秒(这是请求获取所有数据的时间)但是在我将它们放入表视图后,表视图有滞后并且移动缓慢。我想以正确的方式做到这一点。有谁知道怎么做?
这是我试过的:
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()
}
其中fetchPlacesNearCoordinate()是我的函数,它使得google的请求放置api
编辑1
这就是fetchPlacesNearCoordinate()函数的实现方式:
func fetchPlacesNearCoordinate(坐标: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()
}
这里这是我向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)
}
}
}
答案 0 :(得分:0)
你应该有一个看起来像这样的方法
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()
}
}
}
然而如果fetchPlacesNearCoordinate方法没有将clojure / completionBlock的调用分派给主队列,那么它应该看起来像这样
var d = [];
for (var i = 0; i < 3; i++) {
d.push((function(i) {
console.log('iterator: ' + i);
}).bind(this, i));
}
d[0](); // iterator: 0
d[1](); // iterator: 1
d[2](); // iterator: 2