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 Adding our DetailView, Gestures, and More Adding a Gesture Recognizer for Taps

David Lin
David Lin
35,864 Points

Fixing photo flickering and detail view mismatch issues

As implemented in the videos, photos often flicker on presentation, where it appears images are being replaced by others in the view cells. You can see this behavior in the videos too.

Moreover, upon first-time login (after fixing the accessToken issue as presented in the question: Bug in Parsing JSON video: Need to set self.accessToken on first login before calling refreshFoursquare), the photos in the collection view often do not match up with the photos in the detail view.

After debugging, I learned that multiple calls to reloadData in the completion block after getting venue data in the CollectionViewController.refreshFoursquare were causing the issues.

dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });

Every time one venue's data was retrieved, the view controller's data would be reloaded, but it only really needs to be reloaded a single time after all venues' data are received. The rapid, multiple reloadData calls were causing the photo flickering. Moreover, the proper ordering of the photos were being rearranged as well, resulting in photos going to the wrong cells (resulting in mismatch with the detail view), as well as duplicate photos. (Although curiously, this mismatch only occurred on first-time login, not subsequent token-based logins.)

To fix these issues, I limited the reloadData to occur only once by guarding the call with a condition that the count of venueArray be the same as the count of likedArray. Only after each venue's data has arrived will this condition be true, and the view controller will then reloadData once and for all.

    if(self.likedArray.count == self.venueArray.count){
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }