UICollectionViewFooter makes app crash on fast scroll

I have a UICollectionView with a custom footer for section.

The footer contains an UIActivityIndicatorView which spins whenever I'm fetching more data from the server.

The problem is; If I scroll at regular speed through my UICollectionView everything works as expected: when reaching the last row I fetch more data and the footer starts spinning and when the data is fetched the footer disappears.

The problem here is when I fast scroll to the bottom cell , the app crashes with the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind: UICollectionElementKindSectionFooter at path {length = 2, path = 0 - 0}

What may be causing this crash, and why it only happens if I fast down scroll?

Here's the Footer class declaration:

class CustomFooter: UICollectionViewCell {

lazy var activityIndicator: UIActivityIndicatorView = {
    let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
    return indicator
}()

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(activityIndicator)
    activityIndicator.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddinfLeft: 0, paddingBottom: 0, paddingRight: 0, width: 30, height: 30)
    activityIndicator.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    activityIndicator.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    activityIndicator.hidesWhenStopped = true
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func startAnimate() {
    activityIndicator.startAnimating()
}

func stopAnimate() {
    activityIndicator.stopAnimating()
}

}

Here's my UICollectionView code that works with the Footer itself:

let footerId = "footerId"
var footer: CustomFooter?

override func viewDidLoad() {
    super.viewDidLoad()
    //(... other initializations ...)
    collectionView?.register(CustomFooter.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: footerId)
}

override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    // last cell ... fetches more data
    if (indexPath.row == datasource.count - 1) {
    //( fetches more data)
    return
    }
}


override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: footerId, for: indexPath) as! CustomFooter
    self.footer = footer
    return footer
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    if isLoadingData {
        return CGSize.zero
    }
    return CGSize(width: view.frame.width, height: 50)
}


override func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
    if elementKind == UICollectionElementKindSectionFooter {
        guard let footer = self.footer else { return }
        footer.startAnimate()
    }
}


override func collectionView(_ collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, at indexPath: IndexPath) {
    if elementKind == UICollectionElementKindSectionFooter {
        guard let footer = self.footer else { return }
        footer.stopAnimate()
    }
}