AndriousSolutions / ads

Other
58 stars 11 forks source link

Plugin performance #22

Closed aleksandar-radivojevic closed 4 years ago

aleksandar-radivojevic commented 4 years ago

Hey guys, I'm having problems with your plugin, when I add ads performance of my app drops.

First thing that I've noticed is that dispose() method needs around 3 seconds to dispose ads and while is doing that its using a lot of resources and apps gets laggy.

Second thing.. My main screen is a list of news, and when user tap on news he gets navigated to single news page where I want to show banner ad. If I put init() function in initState() of single news page, sometimes it needs a lot of time in order to load banner and if in meantime I navigate back(Navigator.pop) than banner gets stucked in my main page (when it loads), where I dont want it to be, so I put in in main() function of the app, but now scrolling through main screen(list of news) is kinda choppy..

Please take a look at my code and tell me if I implemented sth wrong or give me some idea how can I optimize code..

 import 'dart:io';
import 'package:ads/ads.dart';
import 'package:flutter/material.dart';
import 'package:firebase_admob/firebase_admob.dart';

class AppAds {
  static Ads _ads;

  static final String _appId = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544~3347511713'
      : 'ca-app-pub-3940256099942544~1458002511';

  static final String _bannerUnitId = Platform.isAndroid
      ? 'ca-app-pub-3940256099942544/6300978111'
      : 'ca-app-pub-3940256099942544/2934735716';

  static void showBanner(
          {String adUnitId,
          AdSize size,
          List<String> keywords,
          String contentUrl,
          bool childDirected,
          List<String> testDevices,
          bool testing,
          State state,
          double anchorOffset,
          AnchorType anchorType}) =>
      _ads?.showBannerAd(
          adUnitId: adUnitId,
          size: size,
          keywords: keywords,
          contentUrl: contentUrl,
          childDirected: childDirected,
          testDevices: testDevices,
          testing: testing,
          state: state,
          anchorOffset: anchorOffset,
          anchorType: anchorType);

  static void init() => _ads ??= Ads(
        _appId,
        bannerUnitId: _bannerUnitId,
        childDirected: false,
        nonPersonalizedAds: false,
        size: AdSize.fullBanner,
        testing: true,
        testDevices: ['GSM9X18A11G11457'],
        analyticsEnabled: false,
      );

  static void dispose() => _ads?.dispose();
}
aleksandar-radivojevic commented 4 years ago

So I use init() in main() function of the app, and showBanner() in initState of single news page.

 @override
  void initState() {
    AppAds.showBanner();
    super.initState();
  }

  @override
  void dispose() {
    AppAds.dispose();
    imageCache.clear();
    super.dispose();
  } 
aleksandar-radivojevic commented 4 years ago

Flutter doctor:

[✓] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.15.1 19B88, locale en-RS) • Flutter version 1.9.1+hotfix.6 at /Users/luja/Desktop/code/flutter • Framework revision 68587a0916 (8 weeks ago), 2019-09-13 19:46:58 -0700 • Engine revision b863200c37 • Dart version 2.5.0

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.0) • Android SDK at /Users/luja/Library/Android/sdk • Android NDK location not configured (optional; useful for native profiling support) • Platform android-29, build-tools 29.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 11.1, Build version 11A1027 • CocoaPods version 1.8.3

[✓] Android Studio (version 3.5) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin version 40.2.2 • Dart plugin version 191.8593 • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] Connected device (1 available) • iPhone 8 • D78B9FDA-C30B-4215-8301-A731D4C3C27E • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-1 (simulator)

Andrious commented 4 years ago

@aleksandar-radivojevic

Excellent, Aleksandar! You're code looks good.

Let's try a few things and see what happens. First, let's not even call dispose() and let the phone 'dispose' of resources itself. It sounds counter-intuitive, I know, but let's see what happens.

Secondly, instead of AdSize.fullBanner, please try the default size, AdSize.banner. With that, I wonder if you'll experience the same laggy performance. It may turn out it's the plugin, firebase_admob, used by this package that may be the culprit, but we'll do some testing here first and see what happens.

Thanks.

Greg

aleksandar-radivojevic commented 4 years ago

@Andrious Well when I dont call dispose() method banner ad stays when navigating back to main screen. I have just changed dispose method to a different function and it's seems like its working better now. Just need a couple of more test to be sure.

static void hideBanner() => ads.removeBannerAd();

This is what I use in dispose() method of single news page

aleksandar-radivojevic commented 4 years ago

I can confirm that app is great again, it's working much better. So I just used ads.removeBannerAd() instead of ads.dispose() and thats all.

But I have one more issue which is that ads sometimes not covering ad area. Please look at screenshots for details, sometimes its covering all available space sometimes not.

In screenshots I user AdSize.smartBanner but issue is exactly the same with banner, fullBanner and all other options. What can be the cause of this?

Screenshot_20191107-181352 Screenshot_20191107-181617

Andrious commented 4 years ago

Excellent! Yes, the dispose() is currently using ads.closeBannerAd(); and not ads.removeBannerAd(); However, dispose() will use ads.removeBannerAd(); in the next release coming out this week. Unlike ads.closeBannerAd(), ads.removeBannerAd() removes the Ad from memory and doesn't reload it back up again.

"...ads sometimes not covering ad area." Well, that is interesting. Sometime it covers the area and sometimes it doesn't??? Strange. Off the top of my head I can only suspect the plugin itself is being a problem. I wonder if the three 'placement options' would allow you to instruct the exact size of the Ad?:
anchorOffset horizontalCenterOffset anchorType

aleksandar-radivojevic commented 4 years ago

Placement options not working for me. It would be nice to have sth like BoxFit.cover :D :D

Andrious commented 4 years ago

:) Yes, yes it would.

aleksandar-radivojevic commented 4 years ago

I'm also getting this warning on IOS simulator: FirebaseAdMobPlugin Size Type: AdSizeType.SmartBanner

Andrious commented 4 years ago

As with the placement problem, I feel you'll find a possible solution with the authors of the plugin, firebase_admob.

Asking the same questions with them may get you immediate answer: New Issue

Possibly you're not the only one with such issues, and has already been asked under issues.