Cancel ads request

633 views
Skip to first unread message

Eugene

unread,
May 28, 2015, 7:43:15 AM5/28/15
to ima...@googlegroups.com
Hi guys
Is there any way to instantly cancel ad request after calling adsLoader.requestAds?
Sometimes I need to break ad loading session and I don't want to destroy adsLoader, just cancel request without waiting for adsManagerLoaded or adError events

Vu Chau (IMA SDK Team)

unread,
May 28, 2015, 5:18:03 PM5/28/15
to ima...@googlegroups.com
Hi Eugene,

Normally for that purpose you would destroy the adsManager and call contentComplete() on the adsLoader.  However, that's not possible in this case since the adsManager doesn't exist yet.  We don't have any way to cancel an in-progress ad request.

Let us know if you have other questions,

Vu Chau
IMA SDK Team

Frederic Beleteau

unread,
Jun 5, 2015, 8:01:30 AM6/5/15
to ima...@googlegroups.com
Hi Vu,

I am working on an sdk for our customers, a video player framework.
We also need this behavior because when the customer release our sdk, it could be at the stage of ad request just sent.
But in between, if we destroy all objects around, we can see the adsLoader adsLoadedWithData being called after and trying to work with deallocated objects which will result into severe crashes.
So please, could you add this cancelation step directly inside your adsLoader at dealloc level or at least provide a callback to apply such process on a request we need to cancel manualy.
Many thanks

Frederic Beleteau
MediaDev Team / Akamai Technologies

Frederic Beleteau

unread,
Jun 5, 2015, 8:36:13 AM6/5/15
to ima...@googlegroups.com
To follow up on my previous message, I found a workaround by testing internal variables in my IMA proxy class, if nil that means we can drop at the beginning of adsLoader adsLoadedWithData because all objects are deallocated.
That 's ok, it prevents this class to call objects that do not exist anymore.

However, while doing more QA on this I found the following crashes in the current context, your sdk tries to reach some objects like the webView which might be released as well:

#1 0x00000001000f4818 in -[IMAAdsLoader handleMessageAdsLoaded:] ()
#2 0x0000000186a122c4 in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
#3 0x000000018694f450 in _CFXNotificationPost ()
#4 0x000000018787ea80 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#5 0x00000001000f15f8 in -[IMAJavascriptSession didReceiveMessage:] ()
#6 0x00000001000f08ec in -[IMAJavascriptDispatcher processNewMessage:] ()
#7 0x00000001000f0444 in -[IMAJavascriptDispatcher processNewMessageWithChannelName:data:] ()
#8 0x00000001000efe64 in -[IMAJavascriptBridge webView:didReceiveMessage:] ()
#9 0x0000000100105764 in -[IMAWKWebView userContentController:didReceiveScriptMessage:] ()
#10 0x000000018bfef504 in ScriptMessageHandlerDelegate::didPostMessage(WebKit::WebPageProxy&, WebKit::WebFrameProxy&, WebCore::SerializedScriptValue&) ()
#11 0x000000018c05bb24 in WebKit::WebUserContentControllerProxy::didPostMessage(IPC::Connection*, unsigned long long, unsigned long long, unsigned long long, IPC::DataReference const&) ()

So what can I do to stop this.
As you can see,, if we had the ability to cancel an ad request in progress and deallocate everything safely, that will help here!
Many thanks

Frederic Beleteau
MediaDev Team / Akamai Technologies

Shawn Busolits (IMA SDK Team)

unread,
Jun 5, 2015, 11:28:41 AM6/5/15
to ima...@googlegroups.com, frederic...@gmail.com
Hi Frederic,

I'll talk to the team about this but I'm not sure of the feasibility given the current structure of the SDK.

Would it be possible to delay your deallocations until adsLoader.adSLoadedWithData is called? What you could do is when the publisher releases your SDK, just set a flag, e.g. dropAds. Then when adsLoader.adsLoadedWithData is called, you can just check if dropAds is true, and if so take care of your deallocations. 

Thanks,
Shawn Busolits
IMA SDK Team

Frederic Beleteau

unread,
Jun 17, 2015, 6:07:43 AM6/17/15
to ima...@googlegroups.com, frederic...@gmail.com
Hi Shawn, 
thanks for your message, I gave a try and I am stuck on that.

I tested several options. We cannot postpone the deallocation inside the dealloc itself.
So I added a callback "readyToRemoveVideoPlayer" and from where we can destroy our sdk and do the deallocation at all stages.
In the console logs, I can see the deallocation in progress after adding several NSLog.
However, when the process is complete and if I deallocate the adsLoader and the parent class embedding the Google IMA logic, I get the following crash at adsLoader.adsLoadedWithData :

#1 0x000000010b1df109 in -[IMAAdsLoader handleMessageAdsLoaded:] ()
#2 0x000000010ea6b54c in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
#3 0x000000010e969a04 in _CFXNotificationPost ()
#4 0x000000010bf46968 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#5 0x000000010b1dc317 in -[IMAJavascriptSession didReceiveMessage:] ()
#6 0x000000010b1db6b0 in -[IMAJavascriptDispatcher processNewMessage:] ()
#7 0x000000010b1db20e in -[IMAJavascriptDispatcher processNewMessageWithChannelName:data:] ()
#8 0x000000010b1dad2e in -[IMAJavascriptBridge userContentController:didReceiveScriptMessage:] ()
#9 0x000000010b53d79e in ScriptMessageHandlerDelegate::didPostMessage(WebKit::WebPageProxy&, WebKit::WebFrameProxy&, WebCore::SerializedScriptValue&) ()

in my code I am doing the following:

- (void)adsLoader:(IMAAdsLoader *)loader adsLoadedWithData:(IMAAdsLoadedData *)adsLoadedData
{
   
NSLog(@"Ad Loaded ads : %@", adsLoadedData.description);
   
[_timeOutHandler invalidate];
    _timeOutHandler
= nil;
    _adLoaded
= YES;

   
if (self.sdk.isShuttingDown) {
       
self.adsLoader.delegate = nil;
        _adRequestInProgress
= NO;
       
[self.apiProxy readyToRemoveVideoPlayer];
       
return;
   
}
...


1/ Without a clean solution to stop any network operations inside the IMA component, it will be difficult to remove the IMA instance safely.
The sdk we built could be destroyed at any time.
The crash still happens when we remove IMA at early stage after the Ad request was just sent.
So even after getting the response, it seems to be too soon to deallocate at this time.
What can I do? Any quick workaround on this?

In our project, we had to manage data downloading in a safer way to be able to cancel any operation in progress.
So with NSOperationQueue and URLConnection, you can cancel a request.

2/ As you can see from this piece of code, I also managed a timeout for the Ad request as there's no timeout mechanism built-in. (Correct me if I am wrong)
Maybe such thing could be a good addition on a next build ?

Thanks.
Regards

Frédéric Beléteau
Media Development Team
Akamai Technologies

Shawn Busolits (IMA SDK Team)

unread,
Jun 17, 2015, 11:23:35 AM6/17/15
to ima...@googlegroups.com, frederic...@gmail.com
Hi Frederic,

It looks like this won't in fact work, as you've discovered - if you nil out the adsLoader in its own delegate then it's going to cause problems when that delegate method returns. I'm still looking into a solution but I don't think the IMA SDK as it is will support deallocation at any random time during the process - once you start an ad request, it expects you to complete it. I'll talk to the team to see if there's anything we can do to support this, but again given the structure of the SDK I don't know that it will be feasible.

Thanks,
Shawn Busolits
IMA SDK Team

Frederic Beleteau

unread,
Jun 17, 2015, 8:22:30 PM6/17/15
to ima...@googlegroups.com, frederic...@gmail.com
Ok thanks.
I reviewed the new samples you updated few days ago.
I found that in the advanced example, you retain the adsLoader outside the videoViewController. Is it a best practice now?
And from the documentation I have read the following :
When you request ads more than once, you need to let the ad server know that these are legitimate requests, and not accidental duplicates. The SDK will do this for you with two quick API calls:
  1. Call destroy() on your AdsManager instance. This prevents any post-rolls from playing when you make the below call to contentComplete(). You'll get a new AdsManager when you make your next request.
  1. Call contentComplete() on your AdsLoader instance. This resets the SDK so the new ad request doesn't look like a duplicate of the previous one.
After making the two calls above, you can call requestAds() on your AdsLoader instance to get another set of ads to play for your new video.

Your sample destroy IMA objects from the videoViewController when interacting with the UI and Done button.
I will test the same with the adsLoader integrated into the videoViewController to check if I can replicate the crashes I am facing into your sample app.

In my latest implementation, the crash happens later into IMA method :  -[IMAWKWebView dealloc] ()
when the the player was deallocated.
I'll try to replicate that on your sample and will share the code.

Thanks
Frederic

Frederic Beleteau

unread,
Jun 18, 2015, 5:26:01 AM6/18/15
to ima...@googlegroups.com
Hello, back on your sample AdvancedExample, I added the following in VideoViewController.m : modified viewWillDisappear:
- (void)viewWillDisappear:(BOOL)animated {
 
[self.contentPlayer pause];
 
// Don't reset if we're presenting a modal view (e.g. in-app clickthrough).
 
if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
   
if (self.adsManager) {
     
[self.adsManager destroy];
     
self.adsManager = nil;
   
}
   
self.contentPlayer = nil;
   
self.adsLoader = nil;
 
}
 
[super viewWillDisappear:animated];
}


and setupIMA method to create the adsLoader on demand:
// Initialize AdsLoader.
- (void)setUpIMA {
   
if (self.adsLoader) {
       
self.adsLoader = nil;
   
}
   
IMASettings *settings = [[IMASettings alloc] init];
    settings
.language = @"en";
   
self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:settings];

 
if (self.adsManager) {
   
[self.adsManager destroy];
 
}
 
[self.adsLoader contentComplete];
 
self.adsLoader.delegate = self;
 
if (self.companionView != nil) {
   
[self setUpCompanions];
 
}
}


and if you test and go back and forth between the samples in the table view,
you will get a crash, sometimes at first attempt, sometimes after a new test or two:
#7 0x0000000109f7b1b1 in -[WKWebView dealloc] ()

I hope you can help fixing this, that's a top priority for us. We already spent hours on this.
I need to find a workaround on this as soon as possible.
Our customers have applications using our sdk on the AppStore and they reported many crashes because of this.
One of the application is like a sport news with articles embedding the player, so an article could be dismissed at any time while the end-user scroll and open new content.

Thanks for your help.
Regards
Frederic Beleteau
Media Development Team
Akamai Technologies

Shawn Busolits (IMA SDK Team)

unread,
Jun 18, 2015, 11:15:45 AM6/18/15
to ima...@googlegroups.com, frederic...@gmail.com
Hi Frederic,

Thanks for the additional info. You should be using one AdsLoader for the entire duration of your app, no matter how many ad requests you are sending. Are you able to work around this problem by re-using your AdsLoader?

Thanks,
Shawn Busolits
IMA SDK Team

Frederic Beleteau

unread,
Jun 18, 2015, 5:12:57 PM6/18/15
to ima...@googlegroups.com, frederic...@gmail.com
Hello,
yes I successfully tested this solution. I created the adsLoader in the main app and passed it to our sdk with a new API.
Now we can destroy all other objects safely without the crashes we got before.
However, it makes it more complicated to integrate the final sdk.
The main application must implement the adsLoader initialization and pass it to our sdk.
I am going to disable IMA in case no adsLoader is set externally, so it will prevent from any crash at deallocation time.

Thanks for your help.
Regards
Frederic Beleteau
Media Development Team
Akamai Technologies


Shawn Busolits (IMA SDK Team)

unread,
Jun 19, 2015, 10:23:51 AM6/19/15
to ima...@googlegroups.com, frederic...@gmail.com
Hi Frederic,

Thanks for the update. The SDK is definitely designed to be used with one AdsLoader per app instance. There are some ad server features, like competitive exclusion, which will actually break if you use more than one AdsLoader. If there's anything more we can do to help let me know.

Thanks,
Shawn Busolits
IMA SDK Team
Reply all
Reply to author
Forward
0 new messages