Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To keep your scrolling smooth regardless of where your data comes from, you need to fetch your data on a separate thread and only update the UI when you have the data in memory. <em>Grand Central Despatch</em> is the way to go. Here's a skeleton which assume you have a <code>self.photos</code> dictionary with a text reference to an image file. The image thumbnail may or may not be loaded into a live dictionary; may or may not be in a filesystem cache; otherwise is fetched from an online store. It could use Core Data, but the key to smooth scrolling is that you don't wait around for the data <em>wherever</em> it comes from.</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Photo Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //identify the data you are after id photo = [self.photos objectAtIndex:indexPath.row]; // Configure the cell based on photo id dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //move to an asynchronous thread to fetch your image data UIImage* thumbnail = //get thumbnail from photo id dictionary (fastest) if (!thumbnail) { //if it's not in the dictionary thumbnail = //get it from the cache (slower) // update the dictionary if (!thumbnail) { //if it's not in the cache thumbnail = //fetch it from the (online?) database (slowest) // update cache and dictionary } } } if (thumbnail) { dispatch_async(dispatch_get_main_queue(), ^{ //return to the main thread to update the UI if ([[tableView indexPathsForVisibleRows] containsObject:indexPath]) { //check that the relevant data is still required UITableViewCell * correctCell = [self.tableView cellForRowAtIndexPath:indexPath]; //get the correct cell (it might have changed) [[correctCell imageView] setImage:thumbnail]; [correctCell setNeedsLayout]; } }); } }); return cell; } </code></pre> <p>If you are using some kind of singleton image store manager, you would expect the manager to deal with the details of cache / database access, which simplifies this example.</p> <p>This part</p> <pre><code> UIImage* thumbnail = //get thumbnail from photo id dictionary (fastest) if (!thumbnail) { //if it's not in the dictionary thumbnail = //get it from the cache (slower) // update the dictionary if (!thumbnail) { //if it's not in the cache thumbnail = //fetch it from the (online?) database (slowest) // update cache and dictionary } } </code></pre> <p>would be replaced with something like</p> <pre><code> UIImage* thumbnail = [[ImageManager singleton] getImage]; </code></pre> <p>(you wouldn't use a completion block as you are effectively providing one in GCD when you return to the main queue)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload