ברוב המקרים שבהם משתמשים ב-IMA SDK, צריך לנהל רק בקשה אחת להצגת מודעה בכל פעם. עם זאת, במקרים מסוימים של שימוש ב-IMA, כמו טעינה מראש של נתוני מודעות לפני שהמשתמש בוחר סרטון, יכול להיות שיהיה צורך לשלוח כמה בקשות בו-זמנית. בקשות להצגת מודעות מתבצעות באופן אסינכרוני, ולכן יכול להיות שיהיה קשה לוודא שמנהל המודעות הנכון משויך להקשר הנכון.
כדי לפשט את התהליך של הבחנה בין כמה מערכות לניהול מודעות, IMA SDK ל-tvOS מאפשר לבעלי אתרים להעביר כל ערך או אובייקט לשדה UserContext של כל בקשה להצגת מודעה. אחר כך אפשר לאחזר את הערך או האובייקט האלה בפונקציית הנציג AdsLoader:AdsLoadedWithData, דרך המאפיין userContext של האובייקט IMAAdsLoadedData.
דוגמה
...
adsLoader = IMAAdsLoader(settings: nil)
adsLoader.delegate = self
let userContextA = {id: "Request A", element: videoElementA}
let userContextB = {id: "Request B", element: videoElementB}
let requestA = IMAAdsRequest(
adTagUrl: ViewController.AdTagURLString,
adDisplayContainer: adDisplayContainer,
contentPlayhead: contentPlayhead,
userContext: userContextA)
let requestB = IMAAdsRequest(
adTagUrl: ViewController.AdTagURLString,
adDisplayContainer: adDisplayContainer,
contentPlayhead: contentPlayhead,
userContext: userContextB)
adsLoader.requestAds(with: requestA)
adsLoader.requestAds(with: requestB)
...
// MARK: - IMAAdsLoaderDelegate
func adsLoader(_ loader: IMAAdsLoader!, adsLoadedWith adsLoadedData: IMAAdsLoadedData!) {
let userContext = adsLoadedData.userContext
print("Loaded ads for ID: " + userContext.id)
adsManager = adsLoadedData.adsManager
adsManager.initialize(with: nil)
}
func adsLoader(_ loader: IMAAdsLoader!, failedWith adErrorData: IMAAdLoadingErrorData!) {
let userContext = adsLoadingErrorData.userContext
print("Error loading ads for ID: " + userContext.id)
}
...