Skip to content

Commit 5a6a75d

Browse files
Unsubscribe the OnAdLoaded handler when the native ad handler disconnects (#104)
* Unsubscribe the OnAdLoaded handler when the native ad handler disconnects Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Guard the OnAdLoaded unsubscription for pre-created ads In the NativeAdView(INativeAd, ContentView) path LoadAd never runs, so _onAdLoaded is null on disconnect. The shipped NativeAd implementations use field-like events where removing null is a no-op, but INativeAd is public, so a custom implementation's event accessor could reject null. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 5daabfb commit 5a6a75d

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/Plugin.AdMob/Platforms/Android/Native/NativeAdHandler.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal partial class NativeAdHandler : ViewHandler<NativeAdView, global::Andro
1313
// The ad outlives the handler on disconnect/reconnect, so the subscriptions
1414
// made in RegisterEventHandlers must be removed to avoid raising events N times.
1515
private INativeAd? _registeredAd;
16+
private EventHandler? _onAdLoaded;
1617
private EventHandler<IAdError>? _onAdFailedToLoad;
1718

1819
public static IPropertyMapper<NativeAdView, NativeAdHandler> PropertyMapper =
@@ -77,11 +78,12 @@ private void LoadAd()
7778
var ad = nativeAdService.CreateAd(adUnitId);
7879

7980
RegisterEventHandlers(ad);
80-
ad.OnAdLoaded += (s, e) =>
81+
_onAdLoaded = (s, e) =>
8182
{
8283
VirtualView.RaiseOnAdLoaded(s, e);
8384
ShowAd(ad);
8485
};
86+
ad.OnAdLoaded += _onAdLoaded;
8587

8688
ad.Load();
8789
}
@@ -127,6 +129,11 @@ private void UnregisterEventHandlers()
127129
return;
128130
}
129131

132+
if (_onAdLoaded is not null)
133+
{
134+
_registeredAd.OnAdLoaded -= _onAdLoaded;
135+
}
136+
130137
_registeredAd.OnAdFailedToLoad -= _onAdFailedToLoad;
131138
_registeredAd.OnAdImpression -= VirtualView.RaiseOnAdImpression;
132139
_registeredAd.OnAdClicked -= VirtualView.RaiseOnAdClicked;
@@ -135,6 +142,7 @@ private void UnregisterEventHandlers()
135142
_registeredAd.OnAdClosed -= VirtualView.RaiseOnAdClosed;
136143

137144
_registeredAd = null;
145+
_onAdLoaded = null;
138146
_onAdFailedToLoad = null;
139147
}
140148

src/Plugin.AdMob/Platforms/iOS/Native/NativeAdHandler.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal partial class NativeAdHandler : ViewHandler<NativeAdView, Google.Mobile
1717
// The ad outlives the handler on disconnect/reconnect, so the subscriptions
1818
// made in RegisterEventHandlers must be removed to avoid raising events N times.
1919
private INativeAd? _registeredAd;
20+
private EventHandler? _onAdLoaded;
2021
private EventHandler<IAdError>? _onAdFailedToLoad;
2122

2223
public static IPropertyMapper<NativeAdView, NativeAdHandler> PropertyMapper
@@ -87,11 +88,12 @@ private void LoadAd()
8788
var ad = nativeAdService.CreateAd(adUnitId);
8889

8990
RegisterEventHandlers(ad);
90-
ad.OnAdLoaded += (s, e) =>
91+
_onAdLoaded = (s, e) =>
9192
{
9293
VirtualView.RaiseOnAdLoaded(s, e);
9394
ShowAd(ad);
9495
};
96+
ad.OnAdLoaded += _onAdLoaded;
9597

9698
ad.Load();
9799
}
@@ -170,6 +172,11 @@ private void UnregisterEventHandlers()
170172
return;
171173
}
172174

175+
if (_onAdLoaded is not null)
176+
{
177+
_registeredAd.OnAdLoaded -= _onAdLoaded;
178+
}
179+
173180
_registeredAd.OnAdFailedToLoad -= _onAdFailedToLoad;
174181
_registeredAd.OnAdImpression -= VirtualView.RaiseOnAdImpression;
175182
_registeredAd.OnAdClicked -= VirtualView.RaiseOnAdClicked;
@@ -178,6 +185,7 @@ private void UnregisterEventHandlers()
178185
_registeredAd.OnAdClosed -= VirtualView.RaiseOnAdClosed;
179186

180187
_registeredAd = null;
188+
_onAdLoaded = null;
181189
_onAdFailedToLoad = null;
182190
}
183191

0 commit comments

Comments
 (0)