Reputation: 3961
In my project, I am presenting a View Controller (firePromo()) in viewDidAppear based on certain criteria within the App itself. I've just finished integrating another View Controller (firePromo2()) that is to be presented in viewDidAppear.
Both View Controllers function and display as intended... But there is a chance that both View Controllers will be triggered at the same time on a launch. Granted - probability is low, but there's still a chance.
My code is below:
func firePromo() {
hasShownPromo = true
// ...
self.present(promoView, animated: true, completion:nil)
}
func firePromo2() {
hasShownPromo2 = true
// ...
self.present(promoView2, animated: true, completion:nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if hasShownPromo == false {
if !UserDefaults.standard.bool(forKey: "hasWatchedPromo") {
let promoLaunchCounterFromKey = UserDefaults.standard.integer(forKey: "launchCountForPromoKey")
if promoLaunchCounterFromKey == 4 || promoLaunchCounterFromKey == 9 || promoLaunchCounterFromKey == 14 {
firePromo()
}
}
}
if hasShownPromo2 == false {
if !UserDefaults.standard.bool(forKey: "hasWatchedPromo2") {
let promoLaunchCounterFromKey2 = UserDefaults.standard.integer(forKey: "launchCountForPromoKey2")
if (promoLaunchCounterFromKey2 % 5) == 0 {
firePromo2()
}
}
}
}
I realize that with the above code, it appears that firePromo() & firePromo2() won't evaluate at the same time (5 != divisible by 4 / 9 / 14) - however, there's extra logic involved with promo2 in appDelegate. So there is a chance both will evaluate.
What is the best way to safely handle the presentation of these two View Controllers in the event that both WILL fire at the same time?
Upvotes: 0
Views: 113
Reputation: 149
EDIT: Early return method.
if promoLaunchCounterFromKey == 4 || promoLaunchCounterFromKey == 9 || promoLaunchCounterFromKey == 14 {
firePromo()
return
}
Add a return after firePromo2()
too. Just a suggestion though; segregate all the fire promo code into a distinct method and call the method from viewDidAppear:
. That way, when the method returns early, you are not blocking viewDidAppear:
.
Upvotes: 1