问题
I have one weird issue right now. I am using DispatchGroup()
to verify in-app subscriptions on load. It works only if I have already authenticated with apple id.
let dispatchGroup = DispatchGroup()
var index = 0 // <--- Just For test
for a in inAppPurchaseIds {
index = index + 1
print("INDEX ENTER --> \(index)")
dispatchGroup.enter() // <<---
MPInAppPurchases.verifySubscription(a) { [weak self] status, data, error, expDate in
print("LOOP COUNT --> YES")
guard let me = self else {
print("INDEX LEAVE ME --> \(a)")
dispatchGroup.leave()
return
}
print("DATA ---- \(String(describing: data))")
print("INDEX LEAVE --> \(a)")
dispatchGroup.leave()
}
}
dispatchGroup.notify(queue: .main) {
print("NOTIFY")
}
Function that verifies receipt
static func verifySubscription(_ id: String, completion: purchaseHandler?) {
let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: sharedSecret)
SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
switch result {
case .success(let receipt):
// Verify the purchase of a Subscription
let purchaseResult = SwiftyStoreKit.verifySubscription(
ofType: .autoRenewable, // or .nonRenewing (see below)
productId: id,
inReceipt: receipt)
switch purchaseResult {
case .purchased(let expiryDate, _):
print("\(id) is valid until \(expiryDate)")
completion?(true, "Product Purchased", nil, expiryDate)
case .expired(let expiryDate, _):
print("\(id) is expired since \(expiryDate)")
completion?(false, "Product Expired", nil, expiryDate)
case .notPurchased:
print("The user has never purchased \(id)")
completion?(false, "Product Not Purchased", nil, nil)
}
case .error(let error):
print("Receipt verification failed: \(error)")
completion?(false, "Receipt verification failed", error.localizedDescription, nil)
}
}
}
LOG
INDEX ENTER --> 1
INDEX ENTER --> 2
INDEX ENTER --> 3
LOOP COUNT --> YES
DATA ---- Optional("Product Not Purchased")
INDEX LEAVE --> kr.co.proPackage
The function returns data on completion but here I am unable to get any data from the function and only once it leaves the group. Any idea why this happens? This does happens only when I am not logged in with my apple id in my device. Once its done, it works perfectly.
Steps to reproduce the issue
Log out from my apple id > remove an app from my device > relaunch the app.
来源:https://stackoverflow.com/questions/55294986/dispatchgroup-issue-when-authenticate-apple-id