我從網上獲取JSON數據,解析它,然後使用它在地圖上顯示引腳。NSURLSessionDataTask表現可疑緩慢
這裏是方法之一,其中不存在與沒有問題:
NSString *CLIENT_ID = @"SECRET_ID";
NSString *CLIENT_SECRET = @"CLIENT_SECRET";
NSString *SEARCH = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?near=gjovik&query=cafe&client_id=%@&client_secret=%@&v=20140119", CLIENT_ID, CLIENT_SECRET];
NSURL *searchResults = [NSURL URLWithString:SEARCH];
NSData *jsonData = [NSData dataWithContentsOfURL:searchResults];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.venues = dataDictionary[@"response"][@"venues"];
[self loadAnnotationsAndCenter:YES];
[self loadAnnotationsAndCenter:YES];
檢索來自JSON文件lat和LNG和使用它來在地圖上顯示的引腳。
我決定改變我的代碼NSURLSession。下面是它的樣子:
NSString *CLIENT_ID = @"SECRET_ID";
NSString *CLIENT_SECRET = @"CLIENT_SECRET";
NSString *SEARCH = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?near=gjovik&query=cafe&client_id=%@&client_secret=%@&v=20140119", CLIENT_ID, CLIENT_SECRET];
NSURL *URL = [NSURL URLWithString:SEARCH];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
self.venues = dataDictionary[@"response"][@"venues"];
[self loadAnnotationsAndCenter:YES];
}];
[task resume]
NSURLSession比第一種方法慢10到30秒。我不明白這是爲什麼。在這種情況下,搜索字符串將返回27個不同的位置,如果那麼重要的話
乾杯。
謝謝!這可能是一個愚蠢的問題,但我是IOS新手。我如何知道我的函數是UIKit方法的一部分? –
@ Nilzone-如果您更新UI,即它是以下類別之一:https://developer.apple.com/library/ios/documentation/uikit/reference/UIKit_Framework/_index.html – Rob
@Rob明白了!謝謝! :) –