Note that there are some explanatory texts on larger screens.

plurals
  1. PONSManagedObject's KVO method calls for external binary data attributes make table view scrolling sluggish, why?
    primarykey
    data
    text
    <p>After weeks of debugging, I finally found the culprits of the sporadic lags during my table view scrolling. They are <code>-willChangeValueForKey:</code> and <code>-didChangeValueForKey:</code> calls for an external binary data attribute.</p> <p>For convenience, you can think of my app as a Twitter client. So the major entity is <code>Tweet</code> which has a <code>thumbnail_pic_data</code> attribute that I set to <code>allowsExternalBinaryDataStorage</code>. A corresponding non-persistent property called <code>thumbnail_picture</code> is used as its convenient accessor.</p> <p>The table view is a tweet list view — the timeline, with every tweet's <code>thumbnail_picture</code> displayed inline of its corresponding cell. Pictures are downloaded lazily. After successful downloading, I set <code>thumbnail_pic_data</code> with a custom setter as below:</p> <pre><code>- (void)setThumbnail_pic_data:(NSData *)thumbnail_pic_data { [self willChangeValueForKey:@"thumbnail_pic_data"]; // culprit [self setPrimitiveThumbnail_pic_data:thumbnail_pic_data]; [self didChangeValueForKey:@"thumbnail_pic_data"]; // culprit UIImage *picture; if (thumbnail_pic_data) { picture = [UIImage imageWithData:thumbnail_pic_data]; } self.thumbnail_picture = picture; } </code></pre> <p>With the code above, I see sporadic lags during my table view scrolling after each picture downloading. After I comment out <code>willChangeValueForKey:</code> and <code>didChangeValueForKey:</code> calls, the lags are gone. So I know they are the culprits.</p> <p>However, the timing results I got with the follow code shows that they are not <em>directly</em> using the CPU for a long time:</p> <pre><code>CFAbsoluteTime t1 = CFAbsoluteTimeGetCurrent(); [self willChangeValueForKey:@"thumbnail_pic_data"]; CFAbsoluteTime t2 = CFAbsoluteTimeGetCurrent(); NSLog(@"willChangeValueForKey time: %f", t2 - t1); [self setPrimitiveThumbnail_pic_data:thumbnail_pic_data]; CFAbsoluteTime t3 = CFAbsoluteTimeGetCurrent(); NSLog(@"setPrimitiveThumbnail_pic_data time: %f", t3 - t2); [self didChangeValueForKey:@"thumbnail_pic_data"]; CFAbsoluteTime t4 = CFAbsoluteTimeGetCurrent(); NSLog(@"didChangeValueForKey time: %f", t4 - t3); </code></pre> <p>Timing results:</p> <blockquote> <p>willChangeValueForKey time: 0.000145<br> setPrimitiveThumbnail_pic_data time: 0.001512<br> didChangeValueForKey time: 0.001810 </p> <p>willChangeValueForKey time: 0.000138<br> setPrimitiveThumbnail_pic_data time: 0.001418<br> didChangeValueForKey time: 0.002211 </p> <p>willChangeValueForKey time: 0.000302<br> setPrimitiveThumbnail_pic_data time: 0.001891<br> didChangeValueForKey time: 0.003349 </p> <p>willChangeValueForKey time: 0.000162<br> setPrimitiveThumbnail_pic_data time: 0.001462<br> didChangeValueForKey time: 0.002114</p> </blockquote> <p>As a temporary workaround, I'll leave those two KVO calls commented out. I'm not sure whether any bad thing would happen for <code>Core Data Programming Guide</code> says:</p> <blockquote> <p>You must ensure that you invoke the relevant access and change notification methods (willAccessValueForKey:, didAccessValueForKey:, willChangeValueForKey:, didChangeValueForKey:, willChangeValueForKey:withSetMutation:usingObjects:, and didChangeValueForKey:withSetMutation:usingObjects:).</p> </blockquote> <p>Most importantly, I want to know why those two KVO calls makes table view scrolling sluggish so that hopefully I can find a better workaround.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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