Search code examples
iosobjective-cflickr

NSDictionary constant looping


SO I have a program which calls the FlickR API, gets the URL's puts them into a dictionary and then assigns them into a table view, using an image view.

NSArray *photos = [self.flickr photosForUser:@"James Kinvig"];
    int countAttempts = 0;
    [[self.flickr photosForUser:@"James Kinvig"]count];
    for (int i = 0; i < [[self.flickr photosForUser:@"James Kinvig"]count]; i++) {

        for(NSDictionary *dictionary in photos) {
            countAttempts++;

            NSString *farmId = [dictionary objectForKey:@"farm"];
            NSString *serverId = [dictionary objectForKey:@"server"];
            NSString *photoId = [dictionary objectForKey:@"id"];
            NSString *secret = [dictionary objectForKey:@"secret"];

            self.url= [NSURL URLWithString:[NSString stringWithFormat:@"http://farm%@.staticflickr.com/%@/%@_%@.jpg", farmId, serverId, photoId, secret]];
            //NSLog(@"self.url = %@", self.url);
            NSLog(@"count = %d", countAttempts);

            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
            dispatch_async(queue, ^{

                NSData *imgData = [NSData dataWithContentsOfURL:self.url];

                dispatch_sync(dispatch_get_main_queue(), ^{

                    UIImage *img = [UIImage imageWithData:imgData];
                    cell.imageView.image = img;
                    [cell setNeedsLayout];
                });

            });

            }
    }
return cell;
}

This is the method it calls, photosForUser:

- (NSMutableArray *) photosForUser: (NSString *) friendUserName
{
    NSString *request = [NSString stringWithFormat: @"https://api.flickr.com/services/rest/?method=flickr.people.findByUsername&username=%@", friendUserName];
    NSDictionary *result = [self fetch: request];
    NSString *nsid = [result valueForKeyPath: @"user.nsid"];

    request = [NSString stringWithFormat: @"https://api.flickr.com/services/rest/?method=flickr.photos.search&per_page=%ld&has_geo=1&user_id=%@&extras=original_format,tags,description,geo,date_upload,owner_name,place_url", (long) self.maximumResults, nsid];

    result = [self fetch: request];
    return [result valueForKeyPath: @"photos.photo"];

}

Which does a fetch to the flickr API.

What is happening though is that is stuck in an eternal loop. Even with the for statement being less than the count, it still eternal loops. I have NSLog'd the count of the FlickR photos and it = 11.

This may have something to do with it, but whenever I press the button to take me to the table view controller, I get a HUGE lag, close to a minute, and nothing is being calculated (photo-wise) as I've done a count++

Thanks


Solution

  • let me understand this.. By the last line of your first block of code, I conclude that that is the uitableview dataSource method, cellForRowAtIndexPath.. what doesn't really makes sense.. you you have a fetch there, you have A loop inside a loop, that is setting many images (by download them) in one single imageView, and this is happening for all your visible cells at the same time. This will never work!

    The solution is: 1 - remove this method from the cellForRow, this is not the place to request the images

    2 - create another method that will fetch the content

    3 - create a method that will do your loops and store the images on the array so you don't need to do that many times, only one..

    4 - reload the tableview after you finish the step 3

    5 - use the array of images that is already done to set your images by indexPath.row in your cell..

    6 - I recommend you to use a Library for imageCache (i.e https://github.com/rs/SDWebImage)