attempt to delete item 30 from section 0 which only contains 27 items before the update

142 Views Asked by At

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete item 30 from section 0 which only contains 27 items before the update'

I am facing a problem when collection view reloads my app is getting crash. My code logic is, I am displaying some cells and for every 10 cells am adding one extra cell for advertisement and i think here am getting problem because the indexpath is missing when collection view reloads. I have googled and got some information regarding my crash issue . Some of it are, The documentation for performBatchUpdates:completion states:

Deletes are processed before inserts in batch operations. This means the indexes for the deletions are processed relative to the indexes of the collection view’s state before the batch operation, and the indexes for the insertions are processed relative to the indexes of the state after all the deletions in the batch operation.

I cannot able to implement it in my code. Can anyone please help me in this? Thanks.

 override func viewDidLoad(){

   super.viewDidLoad()

   DispatchQueue.main.async {

   self.viewModel.tilbudsappenModel.addProduct(userId: self.userInfo.userID, shopId: self.shopId)            
        self.m_CollectionVw.reloadData()
    }
}

override func viewWillAppear(_ animated: Bool){
SwiftEventBus.onMainThread(self, name: "ReloadTblVw") { _ in
        self.m_CollectionVw.reloadData()
    }
if self.isFollowed == true {
        viewModel.tilbudsappenModel.removeProduct()
        DispatchQueue.main.async {
            self.viewModel.tilbudsappenModel.addProduct(userId: self.userInfo.userID, shopId: self.shopId)
            self.m_CollectionVw.reloadData()
        }
    }
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // Here am getting the total items count
    numberOfOffers = viewModel.tilbudsappenModel.adNativeModel.totalItems()
    let totalNoOfOffers = numberOfOffers + (numberOfOffers / numberOfAds)
    return totalNoOfOffers        
 }


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.row != 0 && indexPath.row % numberOfAds == 0 && !AppUserDefaults.SharedInstance.isSubscripted {
        let nativeAd: GADUnifiedNativeAd? = nil
        /// Set the native ad's rootViewController to the current view controller.
        nativeAd?.rootViewController = self

        let nativeAdCell = collectionView.dequeueReusableCell(withReuseIdentifier: "UnifiedNativeAdCell", for: indexPath)

        // Get the ad view from the Cell. The view hierarchy for this cell is defined in
        // UnifiedNativeAdCell.xib.
        let adView : GADUnifiedNativeAdView = nativeAdCell.contentView.subviews
            .first as! GADUnifiedNativeAdView

        if self.nativeAds.count > indexPath.row / numberOfAds {

            let nativeAd = self.nativeAds[indexPath.row / numberOfAds]
            adView.nativeAd = nativeAd

            nativeAd.delegate = self

            (adView.headlineView as? UILabel)?.text = nativeAd.headline
            adView.mediaView?.mediaContent = nativeAd.mediaContent
            // These assets are not guaranteed to be present. Check that they are before
            // showing or hiding them.
            (adView.bodyView as? UILabel)?.text = nativeAd.body
            adView.bodyView?.isHidden = nativeAd.body == nil

            (adView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
            adView.callToActionView?.isHidden = nativeAd.callToAction == nil

            (adView.iconView as? UIImageView)?.image = nativeAd.icon?.image
            adView.iconView?.isHidden = nativeAd.icon == nil

            (adView.starRatingView as? UIImageView)?.image = self.imageOfStars(from:nativeAd.starRating)
            adView.starRatingView?.isHidden = nativeAd.starRating == nil

            (adView.storeView as? UILabel)?.text = nativeAd.store
            adView.storeView?.isHidden = nativeAd.store == nil

            (adView.priceView as? UILabel)?.text = nativeAd.price
            adView.priceView?.isHidden = nativeAd.price == nil

            (adView.advertiserView as? UILabel)?.text = nativeAd.advertiser
            adView.advertiserView?.isHidden = nativeAd.advertiser == nil

            // In order for the SDK to process touch events properly, user interaction should be disabled.
            adView.callToActionView?.isUserInteractionEnabled = false

        }

        return nativeAdCell
    } else {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ShopOfferCell
        cellIndex = indexPath.item - indexPath.item / numberOfAds

        if let offerContent = viewModel.tilbudsappenModel.getProductItem(index: cellIndex) {
            cell.setContent(content: offerContent)
            if offerContent.imageWidth < Int(cell.contentView.frame.size.width) {
                cell.m_ImgVw.contentMode = .center
            }else {
                cell.m_ImgVw.contentMode = .scaleAspectFit
            }
        }
        return cell

    }

}

0

There are 0 best solutions below