title: negative or zero sizes are not supported in the flow layout
body: <p>I got a rather complicated collectionView cell and I've noticed that if I scroll really fast on my collectionView it crashes the app.</p>
  
  <p>One of the error I got is this:</p>
  
  <pre><code>negative or zero sizes are not supported in the flow layout
  </code></pre>
  
  <p>I noticed if I just return a float value e.g. 500, in my UICollectionView <code>sizeForItemAtIndexPath:</code> method, it doesn't crash.</p>
  
  <p>My collection view has dynamic cell heights.</p>
  
  <p>I'm parsing HTML Attributed string in my <code>sizeForItemAtIndexPath:</code> method using this library:</p>
  
  <p><a href="https://github.com/mmislam101/HTMLAttributedString">https://github.com/mmislam101/HTMLAttributedString</a></p>
  
  <p>Anyone know what causes the above error message to occur in particular?</p>
  
  <h2>Update</h2>
  
  <p>The other error I also see in my Bugsense report related to this crash is:</p>
  
  <p>-[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]</p>
  
  <p>This happens when I'm scrolling too fast = /</p>
  
  <h2>Update 2</h2>
  
  <p>Bugsense stacktrace shows the crash order method calls is:</p>
  
  <p>1) collectionView:layout:sizeForItemAtIndexPath:</p>
  
  <p>2) calculateFeedCellHeightForIndexPath: (this is one of my own method, not Apple's)</p>
  
  <p>3) dynamicHeightForHTMLAttributedString:UsingWidth:AndFont: (this is one of my own method, not Apple's)</p>
  
  <p>4) HTMLAttributedString attributedStringWithHtml:andBodyFont: line 35</p>
  
  <p>5) HTMLAttributedString attributedString line 79</p>
  
  <p>6) scrollViewDidScroll: line 174</p>
  
  <p>7) setFeedFooterHeight:animated: line 124</p>
  
  <p>My setFeedFooterHeight:animated: method is:</p>
  
  <pre><code>-(void)setFeedFooterHeight:(CGFloat)newHeight animated:(BOOL)animated
  {
      footerHeightConstraint.constant = newHeight;
  
      if(animated)
      {
          [UIView animateWithDuration:0.35 animations:^{
              [self layoutIfNeeded];  // &lt;------ crash on this line it seems
          }];
      }
      else
      {
          [self.feedFooterView layoutIfNeeded];
      }
  }
  </code></pre>
  
  <p>However, when I run the app straight from Xcode rather than Testflight as above, Xcode stops at step 5 above, which yields this piece of code:</p>
  
  <pre><code>- (NSAttributedString *)attributedString
  {
      __block NSString *css       = @"&lt;style&gt;";
  
      [_cssAttributes enumerateObjectsUsingBlock:^(NSString *cssAttribute, NSUInteger idx, BOOL *stop) {
          css = [css stringByAppendingString:cssAttribute];
      }];
  
      css                         = [css stringByAppendingString:@"&lt;/style&gt;"];
      NSString *htmlBody          = [_html stringByAppendingString:css];
  
      NSStringEncoding encoding   = NSUnicodeStringEncoding;
      NSData *data                = [htmlBody dataUsingEncoding:encoding];
  
      NSDictionary *options       = @{NSDocumentTypeDocumentAttribute         : NSHTMLTextDocumentType,
                                      NSCharacterEncodingDocumentAttribute    : @(encoding)};
  
      // app crashes here on this next line.
      NSAttributedString *body    = [[NSAttributedString alloc] initWithData:data
                                                                  options:options
                                                       documentAttributes:nil
                                                                    error:nil];
  
      return body;
  }
  </code></pre>
  
  <p>I read on other threads something about wrapping uicollectionview reload until uicollection.isTracking becomes false?</p>
  
  <p>I tried that but didn't seem to help.</p>
  
  <h2>Update 3</h2>
  
  <p>OK, I accidentally stumble upon the cause of that error.</p>
  
  <p>It's related to the <code>[collectionView.collectionFlowLayout invalidateLayout];</code> call.</p>
  
  <p>I added a 1.0 second delay and the problem appears to be gone.</p>