NSURLSessionDataTask可疑地运行缓慢 [英] NSURLSessionDataTask acting suspiciously slow

查看:165
本文介绍了NSURLSessionDataTask可疑地运行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Web上获取JSON数据,进行解析,然后使用它来显示地图上的图钉.

I'm getting JSON data from the web, parse it, then using it to diplay pins on a map.

这是方法一,没有问题:

Here is method one, which there is no problem with:

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文件中检索经纬度,并使用它们在地图上显示图钉.

[self loadAnnotationsAndCenter:YES]; retrieves the lat and lng from the JSON file and uses it to display pins on the map.

我决定使用NSURLSession更改代码.看起来是这样的:

I decided to change my code uing NSURLSession. Here's what it looks like:

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个不同的位置

NSURLSession is between 10 and 30 seconds slower than the first method. I don't understand why that is. The search string, in this case, returns 27 different locations, if that matters

干杯.

推荐答案

您需要确保UIKit方法将在主线程上执行.

完成处理程序的块将在代理队列"上执行(请参见

The block of the completion handler will be executed on the "delegate queue" (see delegateQueue property of NSURLSession).

您可以通过两种方式完成此操作:设置作为主队列的委托队列([NSOperationQueue mainQueue])或使用

You can accomplish this in two ways: either setup the delegate queue which is the main queue ([NSOperationQueue mainQueue]) or use

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self loadAnnotationsAndCenter:YES]; 
});

在您的完成块中.

这篇关于NSURLSessionDataTask可疑地运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆