Fix "Nothing to Restore" even when transactions exist #640
+23
−14
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.
Hi,
I got the same problem with #620 on my macOS app.
This PR is for fixing that.
Problem details
StoreKit calls the following delegate methods of SwiftyStoreKit's
PaymentQueueController
when restoring transactions is successful:paymentQueue(_:, updatedTransactions:)
paymentQueueRestoreCompletedTransactionsFinished(_:)
I confirmed that these methods are called simultaneously on their own different threads. So if there are so many restored transactions, the code in method 1 takes long time and then the delegate method 2 finishes earlier than 1. In other words, the completion of restoring transactions is finished earlier than the code that processes restored transactions.
It seems that SwiftyStoreKit's
PaymentQueueController
doesn't handle this case correctly andnil
is set torestorePurchases
property ofRestorePurchasesController
before finishing the restoration. I think this is the cause.I'm not sure how many transactions causes this problem. Maybe up to the machine power. This video is when 28 transactions exist:
Screen.Recording.2021-06-14.at.8.18.18.pm_converted.mov
Probably this happens on other platforms too.
Implementation
I newly defined
restorationDispatchQueue
inPaymentQueueController
so that the code of delegate methods 1 and 2 run on the same thread in order.This works fine on my Mac as follows:
Screen.Recording.2021-06-14.at.8.50.22.pm_converted.mov
By the way, maybe other delegate methods, such as
paymentQueue(_: updatedDownloads:)
, also need to be run on the same thread but since I'm neither familiar with StoreKit nor this library and I'm not too sure if it unnecessarily impacts other functionalities or not, I leave them as-is. (But please let me know if it actually doesn't impact or my approach is completely wrong.🙂)Thanks so much for maintaining this great library!