我正在实现In App purchases,它工作得很好。我同时实现了Auto-Renewable和Non-renewable订阅。我可以确定订阅活动和过期的时间。我可以根据订阅状态更新用户配置文件UI,但只能在一次选中一个时执行此操作。
如果我在视图即将出现时同时调用这两个functions。UI变得乱七八糟,无法显示正确的状态。我已经调试并看到,如果第一个检查一个订阅,更新其状态,当我移出view并回来时,它检查另一个返回到默认状态,即standard account,因为Auto-renewable已过期。有没有办法让我同时运行这两个函数,同时使用它们来更新我的UI。下面是我的代码。
var isUserActive: Bool?
var user: User {
return AppDelegate.shared.user
}
func setUserAccountType() {
if self.isUserActive == nil {
self.userAccountType.text = ""
self.userGoGold.isHidden = true
} else {
if self.isUserActive! {
self.userAccountType.text = "Gold Account"
} else {
self.userAccountType.text = "Standard Account"
self.userGoGold.isHidden = false
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.setUserAccountType()
IAPManager.shared.getProducts()
}
override func viewWillAppear(_ pAnimated: Bool) {
super.viewWillAppear(pAnimated)
self.checkForAutoRenewableSubscription()
self.checkForNonRenewableSubscription()
}
func setUpUserAccountStatus(_ pIsActive: Bool) {
DispatchQueue.main.async {
self.isUserActive = pIsActive
self.setUserAccountType()
self.reloadRowForIdentifier(.billing)
self.activityIndicator.hidesWhenStopped = true
self.activityIndicator.stopAnimating()
}
}
func checkForAutoRenewableSubscription() {
self.activityIndicator.startAnimating()
self.user.checkIfSubscriptionIsActive { (pIsActive) in
self.setUpUserAccountStatus(pIsActive)
}
}
func checkForNonRenewableSubscription() {
self.activityIndicator.startAnimating()
self.user.checkifNonRenewableSubscriptionIsActive { (pSubscribed) in
self.setUpUserAccountStatus(pSubscribed)
}
}发布于 2019-03-22 21:47:13
您需要使用DispatchGroup,并在它的notify部分中执行所需的操作
let dispatchGroup = DispatchGroup()
var inpIsActive = false
var inpSubscribed = false
func checkForAutoRenewableSubscription() {
dispatchGroup.enter()
self.activityIndicator.startAnimating()
self.user.checkIfSubscriptionIsActive { (pIsActive) in
self.inpIsActive = pIsActive
self.dispatchGroup.leave()
}
}
func checkForNonRenewableSubscription() {
dispatchGroup.enter()
self.activityIndicator.startAnimating()
self.user.checkifNonRenewableSubscriptionIsActive { (pSubscribed) in
self.inpSubscribed = pSubscribed
self.dispatchGroup.leave()
}
}在viewDidLoad内部
checkForAutoRenewableSubscription()
checkForNonRenewableSubscription()
dispatchGroup.notify(queue: .main) {
self.setUpUserAccountStatus()
}https://stackoverflow.com/questions/55300999
复制相似问题