If you ever get an error message that says “You are corrupting the autolayout engine because you are attempting to update UI elements while not being on the main thread” try putting all your update code in a dispatch main queue. Something like this.

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [SPTMessageSearchAPI fetchTracksForQuery:searchBar.text callback:^(id  _Nullable result, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.tracks = (NSArray *)result;
            [self.collectionView reloadData];
        });
    }];
}

This will let you do other UI elements updates later when re-rendering cells and avoid that corrupt error message.

@implementation SPTMessageTrackCollectionViewCell

- (void)layoutWithTrack:(SPTMessageTrack *)track {
    NSAssert([NSThread isMainThread], @"must be called from main thread");

    // todo - See SpotifyItemCollectionViewCell for example of how to lookup smallest album cover
    self.headerLabel.text = track.title;
    self.subheaderLabel.text = track.artist;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:track.imageUrl]];
}
Advertisement