正确配置闭包以捕获结果

问题描述 投票:0回答:1

我正在为应用内购买编写一些自定义商店方法;一种SwiftyStore的包装器。我遇到的问题是无法在关闭之前从关闭中获取结果。

关于如何正确设置它们的任何建议? IE:封包...

我有一个检查现有订阅的函数,如果它在Firebase中找到一个订阅,则返回true,否则,它将转到苹果商店以验证先前购买的订阅:

func checkSubscription() -> Bool {
        var RetVal: Bool = false
        var retStat: String = ""
        var myVal: Bool = false

        self.rootRef.child("users").child(self.userID!).observeSingleEvent(of: .value, with: { (snapshot) in
            // Get user value
            let value = snapshot.value as? NSDictionary
            let mySubType = value?["subtyp"] as? String ?? ""
            // set value
            if mySubType == "" {
                // get receipt
                if self.myStore.getReceipt() == true {
                    (myVal, retStat) = self.myStore.verifyPurchase(product: "com.xxxxx.xxxxx.monthly")
                    if myVal == true && retStat == "Valid" {
                        // we have a valid product update firebase
                        print("Valid")
                    } else if myVal == true && retStat == "Expired" {
                        // we have a valid product that is expired
                        print("Expired")
                    }
                }
            } else {
                // we have a purchase, verify its not expired.
                print("Purchased")
                RetVal = true
            }

        }) { (error) in
            print(error.localizedDescription)
        }
        return RetVal
    }

这里的问题是在完成关闭操作之前,它会下降到返回RetVal,因此该函数可能会返回无效值。不确定如何在当前设置中解决此问题,但任何建议或建议将不胜感激。

ios swift closures
1个回答
0
投票
func checkSubscription(completion: (_ scuess:Bool) ->()){
  var RetVal: Bool = false
        var retStat: String = ""
        var myVal: Bool = false

        self.rootRef.child("users").child(self.userID!).observeSingleEvent(of: .value, with: { (snapshot) in
            // Get user value
            let value = snapshot.value as? NSDictionary
            let mySubType = value?["subtyp"] as? String ?? ""
            // set value
            if mySubType == "" {
                // get receipt
                if self.myStore.getReceipt() == true {
                    (myVal, retStat) = self.myStore.verifyPurchase(product: "com.xxxxx.xxxxx.monthly")
                    if myVal == true && retStat == "Valid" {
                        // we have a valid product update firebase
                        print("Valid")
                    } else if myVal == true && retStat == "Expired" {
                        // we have a valid product that is expired
                        print("Expired")
                    }
                }
              completion(false)
            } else {
                // we have a purchase, verify its not expired.
                print("Purchased")
                completion(true)
            }

        }) { (error) in
            print(error.localizedDescription)
            completion(false)
        }
        return RetVal
    }

每当您的retValue应该为true时就调用完成(true),而每当您的retValue应该为true时都完成(false)

然后以这种方式调用此函数:

checkSubscription { (sucuess) in
    if(sucuess){
       print("OK")
     }else{
        print("BAD")
     }
 }

0
投票

为了扩展道格拉斯的注释,如果您想在嵌套的异步函数完成后返回结果,则可以传入一个完成处理程序闭包,该闭包使用Swift提供的Result类型,如下所示:

func checkSubscription(completion: (Result<Bool, Error>) -> Void) {
    var RetVal: Bool = false
    var retStat: String = ""
    var myVal: Bool = false

    self.rootRef.child("users").child(self.userID!).observeSingleEvent(of: .value, with: { (snapshot) in
        // Get user value
        let value = snapshot.value as? NSDictionary
        let mySubType = value?["subtyp"] as? String ?? ""
        // set value
        if mySubType == "" {
            // get receipt
            if self.myStore.getReceipt() == true {
                (myVal, retStat) = self.myStore.verifyPurchase(product: "com.xxxxx.xxxxx.monthly")
                if myVal == true && retStat == "Valid" {
                    // we have a valid product update firebase
                    print("Valid")
                } else if myVal == true && retStat == "Expired" {
                    // we have a valid product that is expired
                    print("Expired")
                }
            }
        } else {
            // we have a purchase, verify its not expired.
            print("Purchased")
            RetVal = true
        }

        completion(.success(RetVal))

    }) { (error) in
        print(error.localizedDescription)
        completion(.failure(error))
    }
}

使用这种类型的完成处理程序调用该函数看起来像这样:

checkSubscription { (result) in
    switch result {
    case .success(let boolValue):
        // do something with resulting boolean
        break
    case .failure(let error):
        // do something with resulting error
        break
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.