I'm currently migrating to the StoreKit 2 APIs and all documentation recommend to listen to Transaction.updates as soon as possible (app launch) to not miss any transaction coming in.
"If your app has unfinished transactions, the listener receives them immediately after the app launches. Create a Task to iterate through the transactions from the listener as soon as your app launches."
I'm a bit confused, it doesn't seem very deterministic as it can't technically be the first instruction being executed. I'd love more clarity on that statement.
Is it just a recommended practice and the updates will come in only when a listener is attached to the async sequence?
Question
The larger problem that I'm trying to solve is that I'd like to process unfinished transactions through that listener only when a user is logged in the app, in order to send the Signed Transaction to a custom backend for validation and granting that particular user its purchase (in our own backend database).
If the user is not logged in when opening the app, there will be an undefined time until he/she is logged in, if ever.
What is the best practice to subscribe and handle Transaction.updates in that case? When should this be executed?
My reasoning so far is that we should delay processing transactions until the user is logged in, and query Transaction.unfinished to get the transactions we missed.
// ⬇️ Called when the user is finally logged in.
func processTransactionUpdates() {
// Transaction.updates only receives transactions as they arrive. Any transaction updates received between app launch and login will be lost.
// Transaction.unfinished provides all unfinished transactions,
// which are the ones we need to handle. Finished transactions have already been processed by our backend
// ⬇️ Would processing Transaction.unfinished and start subscribing to updates cover all cases?
Task.detached {
let transactions = await Transaction.unfinished.collect() // <- group the async sequence elements into an array
await self.processUnfinishedTransactions(transactions)
}
Task.detached {
for await transaction in Transaction.updates {
await self.processUnfinishedTransactions([transaction])
}
}
}
I'd love to have more information from someone familiar with the framework and if the above solution would work and is recommended.