title: UICollectionView - attempt to prepare a layout while a prepareLayout call was already in progress - Swift
body: <p>We have a horizontal scrolling collectionView that has 2 sections.</p>
  <p>1st section has cells that are set by array's data and the last section has only 1 cell, that says &quot;Add new item&quot; (which redirects to another viewcontroller)</p>
  <p>To align all these cells, I have set up this code:</p>
  <pre><code>func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  
          // &quot;Add new item&quot; cell
          if indexPath.section == 2 {
              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;cellAdd&quot;, for: indexPath) as! AddCollectionViewCell
              let size = cell.itemText.systemLayoutSizeFitting(UIView.layoutFittingExpandedSize)
              return CGSize(width: size.width, height: 62)
          }
  
          if let items = accountItems {
              let cell = ItemCollectionViewCell()
              let item = items[indexPath.section].names[indexPath.row]
              cell.itemText.text = item.title
              let size = cell.itemText.systemLayoutSizeFitting(UIView.layoutFittingExpandedSize)
              return CGSize(width: size.width+30, height: 62)
          }
          
          return CGSize(width: 100, height: 62)
      }
  </code></pre>
  <p>But when code enters the if-statement (if indexPath.section == 2) in devices with iOS &lt; 11, the app crashes.</p>
  <p>And with the devices with iOS &gt;= 11, warning is thrown saying:</p>
  <blockquote>
  <p>[CollectionView] An attempt to prepare a layout while a prepareLayout call was already in progress (i.e. reentrant call) has been ignored. Please file a bug.</p>
  </blockquote>
  <p>What are the ways to fix this warning and crash it causes on devices with iOS &lt; 11?</p>