Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Thomas Nilsen
14,957 PointsNSURLSession is suspiciously slow
This is a duplicate post, I know, but it wouldn't let me edit it nor add the IOS tag, so:
I used the following code:
NSString *CLIENT_ID = @"SECRET";
NSString *CLIENT_SECRET = @"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];
This worked - no problem. But I decided to switch it up using NSURLSession. The code now looks like this:
NSString *CLIENT_ID = @"SECRET";
NSString *CLIENT_SECRET = @"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 = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
self.venues = dataDictionary[@"response"][@"venues"];
[self loadAnnotationsAndCenter:YES];
}];
[task resume];
Why is NSURLSession so much slower than the first method? We're talking as much as 10-30 seconds slower. Sometimes I get weird errors using the the second piece of code. Am I implementing something wrong?
Cheers!
3 Answers
Chase Lee
29,275 PointsWould you like me to delete your previous post?
J Sherwani
248 PointsThomas,
Which part of the code takes the extra 10-30 seconds? Is it the callback to the completion handler? One way to debug this is through a proxy, where you can inspect exactly what network traffic is flowing through (and how long it's taking). I'd be happy to walk you through this if you're interested — I have some free time right now, and have decided to spend the rest of my evening helping out.
J
Thomas Nilsen
14,957 PointsHi! Sorry for late response (time zones). The code that takes such a long time is this line inside the completion handler
[self loadAnnotationsAndCenter:YES];
Which depends on the
self.venues arrays to be filled up
What's strange is, I have a button which takes to me to a table view, where all the different location names is displayed, meaning the self.venues array has to be complete. So I don't understand why the function [self loadAnnotationsAndCenter:YES]; takes that much time, when the array is filled up.
Thomas Nilsen
14,957 PointsI figured it out! the loadAnnotationsAndCenter method has to run on the main thread. This code solved my problems:
dispatch_async(dispatch_get_main_queue(), ^{
[self loadAnnotationsAndCenter:YES];
});
Thomas Nilsen
14,957 PointsThomas Nilsen
14,957 Pointsyes please
Chase Lee
29,275 PointsChase Lee
29,275 PointsDone. Sorry I don't have much experience with ios. Otherwise I would try to help you.
Thomas Nilsen
14,957 PointsThomas Nilsen
14,957 PointsNo problem. Hopefully someone else will :)