首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在更新UI时同时执行两个功能

如何在更新UI时同时执行两个功能
EN

Stack Overflow用户
提问于 2019-03-22 21:45:44
回答 1查看 110关注 0票数 0

我正在实现In App purchases,它工作得很好。我同时实现了Auto-RenewableNon-renewable订阅。我可以确定订阅活动和过期的时间。我可以根据订阅状态更新用户配置文件UI,但只能在一次选中一个时执行此操作。

如果我在视图即将出现时同时调用这两个functions。UI变得乱七八糟,无法显示正确的状态。我已经调试并看到,如果第一个检查一个订阅,更新其状态,当我移出view并回来时,它检查另一个返回到默认状态,即standard account,因为Auto-renewable已过期。有没有办法让我同时运行这两个函数,同时使用它们来更新我的UI。下面是我的代码。

代码语言:javascript
复制
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)
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-22 21:47:13

您需要使用DispatchGroup,并在它的notify部分中执行所需的操作

代码语言:javascript
复制
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内部

代码语言:javascript
复制
checkForAutoRenewableSubscription()
checkForNonRenewableSubscription() 
dispatchGroup.notify(queue: .main) { 
   self.setUpUserAccountStatus()
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55300999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档