How to wait for restore purchases to finish?

I have a “Restore Purchases” button in the UI which calls IStoreExtension.RestoreTransactions. I understand, that once a successfull request is sent, Unity will call IStoreListener.ProcessPurchase for each restored item.

I would like to wait and detect that all purchases have been restored or have some sort of event that indicates that all products are now refreshed.

When does ProcessPurchase happen after a restore? I assume the call is async since its coming from a web request and that there might even be a multi-frame delay between each invocation for each item, right?

So, how can I know that when ProcessPurchase was called, that it has been the last item or if there are more items coming in a few seconds, etc?

My simplified code looks like:

public class MyStore : MonoBehaviour, IStoreListener
{
    private IExtensionProvider extensions;
    private IReceiptStorage storage;

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        this.extensions = extensions;
    }

    public void OnInitializeFailed(InitializationFailureReason error) { }

    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
    {
        storage.SaveReceipt(
            purchaseEvent.purchasedProduct.definition.id,
            purchaseEvent.purchasedProduct.receipt);
       
        // Now that an item was saved, the UI needs to update,
        // but I want to wait until ALL items were restored and
        // then only update a single time. But how do I know that
        // more items are coming in the future?

        return PurchaseProcessingResult.Complete;
    }

    public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason) { }

    public void RestorePurchases()
    {
        extensions.GetExtension<IAppleExtensions>()
            .RestoreTransactions(
                success => Debug.Log("Restore purchases request sent: " + success));

        // ... now this will call ProcessPurchase for every purchased item. But when?
    }
}

Again, I don’t want to immediately react to an individual purchased item, but instead I need to wait for all products to be processed and only then continue the flow of the UI.

So far my only Idea is to send the restore request and then wait 3 seconds before refreshing the UI and hoping that in the meantime my ReceiptStorage was filled with updated receipts/products.

When the RestoreTransactions method completes, you are done. All ProcessPurchase calls will have been completed at that point.

1 Like

No way, that would be too easy! :smile: Ok, thanks for clarifying!