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