1

コレクションビューで画像とともに広告を表示するために FlurryAd を使用しています。次のコードを使用してバックグラウンド スレッドで広告を取得する

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
        }
    }

広告の準備ができたら、次のコードを使用して、メイン スレッドで表示されているセルをリロードしようとしています。

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)
                }
            }
        }


    }

その後、次のエラーでクラッシュします

******* -[UICollectionView _endItemAnimationsWithInvalidationContext:tempativelyForReordering:animator:] でのアサーションの失敗、/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UICollectionView.m:5781* ***

cellForItemAtindexPath の外観

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
        }

    }
4

0 に答える 0