completeTransactions should finish failed transactions if atomically: false #322
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
Non-Atomic transactions are useful when the application needs to deliver content from the server before finishing a transaction.
When the application starts, it is best practice to call
SwiftyStoreKit.completeTransactions
. This method makes it possible to clear all transactions that may have completed while the app was not running.Crucially, this applies to
.purchased
,.restored
and.failed
transactions. See Apple Docs forSKPaymentQueue
:As of SwiftyStoreKit 0.11.2, calling
SwiftyStoreKit.completeTransactions(atomically: false)
does not callfinishTransactions
on.failed
transactions.This is not in line with
purchaseProduct
andrestorePurchases
, which both callfinishTransactions
on.failed
transactions, regardless of theatomically
flag.Why this matters (side effects)
When transactions are not finished, they hang out in the payment queue indefinitely, and can cause problems with later purchases.
Consider this example:
SwiftyStoreKit.completeTransactions(atomically: false)
.failed
transaction is not finished.completeTransactions
later).How to fix this
Modify the implementation of
completeTransactions
to ensure failed transactions are always finished, even ifatomically: false
.This ensures that all completed transactions are always caught and finished when the app launches, as long as
SwiftyStoreKit.completeTransactions
is called.With this fix, it's also essential that
purchase.needsFinishTransaction
is alwaysfalse
whencompleteTransactions
returns a failed transaction, regardless of the value ofatomically
.For more clarity, the README will be updated to use a
switch
statement in the completion block ofcompleteTransactions
.Note that with this fix, apps using the old sample code from the README should now work correctly: