Reputation: 411
I have an iPad app which uses an open source class MMGridView. Basically I perform a CoreData query, and then this class draws boxes on the screen in a grid pattern to represent the results. The boxes include photos. Everything works fine, except that sometimes there is an unexplained time lag until the UI is drawn on the screen. Sometimes the refresh is instant, sometimes it is over 20 seconds. It appears very random in its behaviour.
I've managed to determine that:
My question is, what happens after DrawRect is called that I can try to look at to identify the problem?
Absolutely any ideas would be greatly appreciated.
Note also that user interaction is able to cut the lag short. - rotating the iPad (triggers redraw) always makes the ui refresh immediately - pressing the button to redo the same query will generally refresh the screen
So it really appears like there is just some random delay I'm hitting, as opposed to any sort of processor/work load issue. Problem is that programmatically, I can't tell if the UI has updated or not. Otherwise I could just ask it to redraw again.
I'm including the code from the DrawRect method just in case that sparks any ideas, but from what I can tell, this method completes before the lag occurs.
- (void)drawRect:(CGRect)rect {
if (self.dataSource && self.numberOfRows > 0 && self.numberOfColumns > 0) {
NSInteger noOfCols = self.numberOfColumns;
NSInteger noOfRows = self.numberOfRows;
NSUInteger cellsPerPage = noOfCols * noOfRows;
NSLog(@"rows = %d, cols = %d", noOfCols, noOfRows);
CGRect gridBounds = self.scrollView.bounds;
CGRect cellBounds = CGRectMake(0, 0, gridBounds.size.width / (float)noOfCols,
gridBounds.size.height / (float)noOfRows);
CGSize contentSize = CGSizeMake(self.numberOfPages * gridBounds.size.width, gridBounds.size.height);
[self.scrollView setContentSize:contentSize];
for (UIView *v in self.scrollView.subviews) {
[v removeFromSuperview];
}
for (NSInteger i = 0; i < [self.dataSource numberOfCellsInGridView:self]; i++) {
MMGridViewCell *cell = [self.dataSource gridView:self cellAtIndex:i];
[cell performSelector:@selector(setGridView:) withObject:self];
[cell performSelector:@selector(setIndex:) withObject:[NSNumber numberWithInt:i]];
NSInteger page = (int)floor((float)i / (float)cellsPerPage);
NSInteger row = (int)floor((float)i / (float)noOfCols) - (page * noOfRows);
CGPoint origin = CGPointMake((page * gridBounds.size.width) + ((i % noOfCols) * cellBounds.size.width),
(row * cellBounds.size.height));
CGRect f = CGRectMake(origin.x, origin.y, cellBounds.size.width, cellBounds.size.height);
cell.frame = CGRectInset(f, self.cellMargin, self.cellMargin);
[self.scrollView addSubview:cell];
}
}
}
Upvotes: 1
Views: 843
Reputation: 411
Ok problem solved due to a very clear and concise description of when to use and when not to use drawRect: by Peter Hosey in a response to this question. What is the proper way to make it so core graphics does not lag?
Basically, this is a bug in the Open source class MMGridView which I am using, because it should not be using drawRect: to setup the boxes or cells on the UIScrollView. This is because there is no actual drawing taking place.
So by changing the name of the method above and calling it directly where I previously called setNeedsDisplay, there is no longer any random lag.
Upvotes: 1