ios - 更新UI时如何同时执行2个函数

标签 ios swift multithreading in-app-purchase

我正在实现In App purchases,效果很好。我正在实现 Auto-RenewableNon-renewable 订阅。我可以确定任一订阅何时处于事件状态以及何时过期。我可以根据订阅状态更新用户配置文件用户界面,但只能在一次检查一个时执行。

如果我在 View 即将出现时调用两个函数。用户界面变得困惑,无法显示正确的状态。我已经调试并看到,如果第一个检查一个订阅,更新它的状态,当我移出 view 并返回时,它会检查另一个返回到默认状态,即 standard account 因为 Auto-renewable 已过期。有没有一种方法可以让我同时运行这两个功能并同时使用它们来更新我的用户界面。下面是我的代码。

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)
    }
}

最佳答案

您需要使用 DispatchGroup 并在其内部通知部分执行您需要的操作

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()
}

关于ios - 更新UI时如何同时执行2个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55300999/

相关文章:

ios - 使用 swift 运行 pod install 时出错

ios - CGImageAlphaInfo.Last 在 iOS 8 中不受支持的参数组合

iphone - 未找到 Restkit.h 文件

ios - 在 UIPanGestureRecognizer 更改状态中更改 UIView 的 alpha

ios - 在当前应用中添加 UIPageViewController

c# - 如何使用多个线程管理从集合中添加和删除特定项目?

ios - 仅用于列出 Uber 产品名称和匹配 ID 的 API

iphone - 为什么我们需要将Apple的In App Payment收据存储到服务器?

c++ - 多线程的 shared_ptr 销毁是否安全?

java - 多线程Java