'StoreKit 2 unexpected behavior with consumable items

I'm trying to implement iAP with StoreKit 2. Maybe my code isn't very elegant but seems to work ;-) But the following behavior I can#t understand: If I first start my app in simulator or on my test device I can bought a consumable item. If I want to buy it again (how consumables should work) the app automatically restores the purchase and don't ask for purchase.

Is this a specific behavior in my test environment or do I understand consumables wrong? Because StoreKit 2 is quite new I haven't found a tutorial in the internet that's fits for me.

Any hints are very wellcome :-)



Solution 1:[1]

Maybe I solved my problem. Can please someone approve it if I'm right?

I added await transaction.finish() to my purchase()

 func purchaseTSBooster() {
        
        self.boughtItem = true
        
        Task.init {
            guard let tsProduct = tsBoosterProducts.first else {return}
            
            do {
                let result = try await tsProduct.purchase()
                switch result {
                    
                case .success(let verification):
                    switch verification {
                        
                    case .verified(let transaction):
                        DispatchQueue.main.async {
                            self.tsBoosterID.append(transaction.productID) 
                        }
                        await transaction.finish()
                    case .unverified(_, _):
                        break
                    }
                case .userCancelled:
                    buyaborted = true
                    
                case .pending:
                    break
                @unknown default:
                    break
                }
            }
            catch {
                
                print(error)
            }
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael