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

iOS Displaying API Data with Collection Views in Objective-C Show Me the Data - API’s, OAuth, and NSURLSession Populating the CollectionView

Evan Goodwin
PLUS
Evan Goodwin
Courses Plus Student 22,260 Points

Repeated pictures of like places, but can't see difference in code.

I have pasted in the ProjectFile version of refreshFoursquare and everything works. My own code leads to repeated photos. I have checked the code difference and the only difference was a misspelling in the ProjectFile version(which works) - requst instead of request (NSURLRequest *requst = [[NSURLRequest alloc]initWithURL:url];) .

Here is the code.

Working Project File:

-(void)refreshFoursquare{
    NSURLSession *session = [NSURLSession sharedSession];
    NSString *urlString = [[NSString alloc]initWithFormat:@"https://api.foursquare.com/v2/users/self/venuelikes/?oauth_token=%@&v=%@&m=%@", self.accessToken, DATA_VERSION_DATE, DATA_FORMAT];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSData *data = [[NSData alloc]initWithContentsOfURL:location];

        NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        self.likedArray = [responseDict valueForKeyPath:@"response.venues.items.id"];

        self.venueArray = [[NSMutableArray alloc]init];

        for(NSString *venueID in self.likedArray){
            NSString *urlStringVenue = [[NSString alloc]initWithFormat:@"https://api.foursquare.com/v2/venues/%@?oauth_token=%@&v=%@&m=%@", venueID, self.accessToken, DATA_VERSION_DATE, DATA_FORMAT];
            NSURL *urlVenue = [NSURL URLWithString:urlStringVenue];
            NSURLRequest *requestVenue = [[NSURLRequest alloc]initWithURL:urlVenue];
            NSURLSessionDownloadTask *taskVenue = [session downloadTaskWithRequest:requestVenue completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                NSData *dataVenue = [[NSData alloc]initWithContentsOfURL:location];
                NSDictionary *responseDictVenue = [NSJSONSerialization JSONObjectWithData:dataVenue options:kNilOptions error:nil];
                [self.venueArray addObject:responseDictVenue];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.collectionView reloadData];
                });
            }];
            [taskVenue resume];
        }


    }];

    [task resume];
}

MIne Code with Repeating Pictures.

-(void)refreshFoursquare{
    NSURLSession *session = [NSURLSession sharedSession];
    NSString *urlString = [[NSString alloc]initWithFormat:@"https://api.foursquare.com/v2/users/self/venuelikes/?oauth_token=%@&v=%@&m=%@", self.accessToken, DATA_VERSION_DATE, DATA_FORMAT];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSData *data = [[NSData alloc]initWithContentsOfURL:location];

        NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        self.likedArray = [responseDict valueForKeyPath:@"response.venues.items.id"];

        self.venueArray = [[NSMutableArray alloc]init];

        for(NSString *venueID in self.likedArray){
            NSString *urlStringVenue = [[NSString alloc]initWithFormat:@"https://api.foursquare.com/v2/venues/%@?oauth_token=%@&v=%@&m=%@", venueID, self.accessToken, DATA_VERSION_DATE, DATA_FORMAT];
            NSURL *urlVenue = [NSURL URLWithString:urlStringVenue];
            NSURLRequest *requestVenue = [[NSURLRequest alloc]initWithURL:urlVenue];
            NSURLSessionDownloadTask *taskVenue = [session downloadTaskWithRequest:requestVenue completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                NSData *dataVenue = [[NSData alloc]initWithContentsOfURL:location];
                NSDictionary *responseDictVenue = [NSJSONSerialization JSONObjectWithData:dataVenue options:kNilOptions error:nil];
                [self.venueArray addObject:responseDictVenue];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.collectionView reloadData];
                });
            }];
            [taskVenue resume];
        }


    }];

    [task resume];
}