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
Adrian Ilie
10,067 PointsUITableView with asynchronous loaded images and stored locally using Core data
I am working on an application that displays a list of venues containing a name, address and an image fetched from a REST Webservice i built. The image is specified as a URL string pointing to an actual file on the server. The images are 180x180 px JPEG compressed.
I am trying to display this list in a UITableView with a custom cell, loading everything asynchronously and storing them using core data. I defined a core data entity with various properties and an “image” property of type “Binary Data” with external storage to hold the downloaded image for each venue.
I tried various approaches but most of them led to a sluggish scroll and a not so good experience.
The best results i got by doing the following:
- create 2 properties in UITableViewController
- NSArray localVenues (holds instances of core data entities without the “image” property)
- NSMutableDictionary localVenuesImages (holds pairs of VenueID - NSData image)
- in viewDidLoad:
- check local storage for any saved entities, if found i load them in localVenues and localVenuesImages
- if no local saved entities are found, the list of venues is downloaded asynchronously and stored in localVenues and in local core data database
- in cellForRowAtIndexPath i pass to the cell (via a function called configureCellForEntry) both localVenues and localVenuesImages
- in configureCellForEntry i do the following
- set all the text labels on the main thread
- dispatch a secondary thread that:
- checks if an image exists in localVenuesImages for the given VenueID, if true it creates a UIImage with it, and switches to the main thread to display it
- if no image is present in localVenuesImages it downloads it, then switches to the main thread and:
- adds it in localVenuesImages
- saves it to it’s corresponding core data entity
- displays it
I am wondering if there’s a better way to do this.
Thank you.