1

I am using FlurryAd to show ads along with images in collectionView. Fetching ads in background thread using following code

func loadAds(count:Int,adType: Int) {
var newAdsList : [FlurryAdNative] = []
        DispatchQueue.global(qos: .background).async {
        if count >= 1 {
            for _ in 1...count {
                let nativeAd = FlurryAdNative(space: AD_SPACEID)
 nativeAd?.adDelegate  = self
                nativeAd?.viewControllerForPresentation = self
                nativeAd?.fetchAd()
                    if let adType = AdType(rawValue: adType) {
                        if adType == .OfferAd {
                            self.viewModel.tilbudsappenModel.adNativeModel.addNativeAds(item: nativeAd!)
                            newAdsList.append(nativeAd!)
                        }
                    }
            }
        }
            self.pendingAdList = newAdsList
        }
    }

When Ad is ready iam trying to reload the visible cells in main thread using following code

func adNativeDidFetchAd(_ nativeAd: FlurryAdNative!) {
        let isScrolling = (self.m_CollectionVw.isDragging || self.m_CollectionVw.isDecelerating)
        if isScrolling == false {
            if let indexPath = self.m_CollectionVw?.indexPathsForVisibleItems {
                DispatchQueue.main.async {
                    self.m_CollectionVw?.reloadItems(at: indexPath)
                }
            }
        }


    }

then getting crash with following error

******* Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UICollectionView.m:5781****

cellForItemAtindexPath looks

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch viewModel.tilbudsappenModel.adNativeModel.cellType(index: indexPath.item + 1) {
        case .Ads:
            let cellIndex = indexPath.row  / viewModel.tilbudsappenModel.adNativeModel.adRangeIndex
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adNativeCell", for: indexPath as IndexPath) as! NativeAdCell
            if  let adItem = viewModel.tilbudsappenModel.adNativeModel.nativeAdsItem(index: cellIndex) {
//                delay(0.01, closure: {
                    cell.setupWithFlurryNativeAd(adNative: adItem)
//                })

            }
            return cell
        case .Normal:
            let cellIndex = (indexPath.item + 1) * (viewModel.tilbudsappenModel.adNativeModel.adRangeIndex - 1)  / viewModel.tilbudsappenModel.adNativeModel.adRangeIndex
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! KategorierFollowCollectionViewCell
            cell.m_Label.text = String(format: "%d", indexPath.row)
            if let productContent = viewModel.tilbudsappenModel.getProductItem(index: cellIndex) {
                let followed = self.viewModel.tilbudsappenModel.getProductFollowed(prodId: productContent.id,userId: userInfo.userID)
                let added = self.viewModel.tilbudsappenModel.getProductAdded(prodId: productContent.id,userId: userInfo.userID)
                cell.setContent(content: productContent, isAdded: added, isFollowed: followed)
            }
            cell.backgroundColor = UIColor.white
            return cell
        }

    }
Ram Mohan
  • 96
  • 6
  • can you share your code for cellForItemAtIndexPath ?? – Hakikat Singh Sep 08 '17 at 08:15
  • Hi, thanks for your quick response. I updated my question with cellForItemAtIndexPath. can you look this – Ram Mohan Sep 08 '17 at 08:22
  • @RamMohan instead of reloading items at visible cells, could you please try self.m_CollectionVw?.reloadData() and see if it still crashs or not – Aravind A R Sep 08 '17 at 08:39
  • @AravindAR i already did that, when i reloadData() it didn't get crash but problem is for every ads func adNativeDidFetchAd(_ nativeAd: FlurryAdNative!) gets called. For example: if list have 10 ads func adNativeDidFetchAd(_ nativeAd: FlurryAdNative!) calls 10 times. so that i am getting some flickering issue and also some ads not loading. Thats why i am trying to reloading items at visible cells – Ram Mohan Sep 08 '17 at 08:53
  • Why are you reloading at all? Why not simply get a reference to the required cell and update the content directly? – Paulw11 Sep 08 '17 at 09:17
  • @Paulw11 can't understand clearly can you please explain clearly. – Ram Mohan Sep 08 '17 at 09:24
  • If you know which rows contain the ads you can use `cellForRow(at:)` on the tableview to retrieve the cell for that row. If you get `nil` then the row is offscreen. If you get a cell, update the content. – Paulw11 Sep 08 '17 at 09:28
  • @Paulw11Its worked, Thank you so much. – Ram Mohan Sep 08 '17 at 10:38

0 Answers0