Friday, August 19, 2022
HomeiOS Developmentswift - iOS Google AdMob advertisements Slowing down efficiency

swift – iOS Google AdMob advertisements Slowing down efficiency

[ad_1]

We’ve adopted this text:

https://github.com/googleads/googleads-mobile-ios-examples/blob/grasp/Swift/superior/BannerTableViewExample/BannerTableViewExample/TableViewController.swift

To combine google commercials on an iOS venture. Once we use the demo advert unit id(/6499/instance/banner) every little thing works nice.

Nonetheless, after we change to manufacturing advertisements our units begin to overheat and the app lags considerably.

The advertisements are displayed in a Assortment View.
We’re caching the advertisements the identical approach with Google’s instance

class AdvertisementManager{
     static let TAG_A = 1
     static let TAG_B = 2
     static let TAG_C = 3
     static let TAG_D = 4
     static let TAG_E = 5
     static let TAG_F = 6
     // .............
}

struct GoogleAdvertisementMetadata {
    weak var delegate: GADBannerViewDelegate?
    weak var adSizeDelegate: GADAdSizeDelegate?
    weak var rootViewController: UIViewController?
    let validAdSizes: AdSizesOption
    let targetingMetadata: AdTargetingMetadata
}

non-public func preLoadAdvertisements() {
    let targetingVals = interactor?.getTargetingValues()
    let targetingMetadata = AdTargetingMetadata(targetingValues: targetingVals)
    let googleAdvertisementMetadata = GoogleAdvertisementMetadata(
    delegate: self,
    adSizeDelegate: self,
    rootViewController: attachedView,
    validAdSizes: advertisementSizes,
    targetingMetadata: targetingMetadata)
    googleMetadata = googleAdvertisementMetadata
    cachedAdvertisements.removeAll()
    cachedAdvertisements = 
    advertisementManager.createAdvertismentsForSections(googleAdvertisementMetadata)
}
func createAdvertismentsForSections(_ googleAdvertisementMetadata: GoogleAdvertisementMetadata) -> [String: GAMBannerView] {
      var cachedAdvertisements: [String: GAMBannerView] = [:]
      let adA = AdvertisementConstants.FeedAdvertisementSectionA
      let adB = AdvertisementConstants.FeedAdvertisementSectionB
      let adC = AdvertisementConstants.FeedAdvertisementSectionC
      let adD = AdvertisementConstants.FeedAdvertisementSectionD
      let adE = AdvertisementConstants.FeedAdvertisementSectionE
      cachedAdvertisements[adA + "(tagA)"] = createAdvertisement(with: adA, tag: tagA, metadata: googleAdvertisementMetadata)
     cachedAdvertisements[adB + "(tagB)"] = createAdvertisement(with: adB, tag: tagB, metadata: googleAdvertisementMetadata)
     cachedAdvertisements[adC + "(tagC)"] = createAdvertisement(with: adC, tag: tagC, metadata: googleAdvertisementMetadata)
     cachedAdvertisements[adD + "(tagD)"] = createAdvertisement(with: adD, tag: tagD, metadata: googleAdvertisementMetadata)
     cachedAdvertisements[adE + "(tagE)"] = createAdvertisement(with: adE, tag: tagE, metadata: googleAdvertisementMetadata)
     return cachedAdvertisements
}

non-public func createAdvertisement(with adID: String, tag: Int, metadata: GoogleAdvertisementMetadata) -> GAMBannerView {
   let view = GAMBannerView(adSize: GADAdSizeMediumRectangle)
   view.adUnitID = adID
   view.tag = tag
   view.delegate = metadata.delegate
   view.adSizeDelegate = metadata.adSizeDelegate
   view.rootViewController = metadata.rootViewController
   view.validAdSizes = getAdSizes(sizeOption: metadata.validAdSizes)
   let request = GAMRequest()
request.customTargeting = AdTargetingManager().makeCustomAdTargetingDictionary(metadata: metadata.targetingMetadata)
view.load(request)
  return view
 }

func setupAdvertisements() {
      datasource = datasource.filter({($0 as? AdvertisementCellConfigurator) == nil})
      let adA = AdvertisementModel(adId: AdvertisementConstants.FeedAdvertisementSectionA, tagCell: AdvertisementManager.TAG_A)
      let adB = AdvertisementModel(adId: AdvertisementConstants.FeedAdvertisementSectionB, tagCell: AdvertisementManager.TAG_B)
      let adC = AdvertisementModel(adId: AdvertisementConstants.FeedAdvertisementSectionC, tagCell: AdvertisementManager.TAG_C)
      let adD = AdvertisementModel(adId: AdvertisementConstants.FeedAdvertisementSectionD, tagCell: AdvertisementManager.TAG_D)
      let adE = AdvertisementModel(adId: AdvertisementConstants.FeedAdvertisementSectionE, tagCell: AdvertisementManager.TAG_E)
datasource.addAdvertisments(firstAdPosition: 3, frequency: 5, advertisements: [adA, adB, adC, adD, adE])
      let adCount = datasource.depend(the place: {$0 is AdvertisementCellConfigurator})
      if adCount > AdvertisementManager.TAG_E {
            datasource = datasource.filter({($0 as? AdvertisementCellConfigurator) == nil})
          var commercials = [adA, adB, adC, adD, adE]
          for counter in (AdvertisementManager.TAG_E + 1)...adCount {
          let adE = AdvertisementConstants.FeedAdvertisementSectionE
          commercials.append(AdvertisementModel(adId: adE, tagCell: counter))
          if counter > cachedAdvertisements.depend {
             cachedAdvertisements[adE + "(counter)" ] = advertisementManager.requestRepeatingAdvertisement(with: adE, metadata: googleMetadata)
         }
        }
       datasource.addAdvertisments(firstAdPosition: 3, frequency: 5, advertisements: commercials)
     }
}

[ad_2]

RELATED ARTICLES

Most Popular

Recent Comments