Prepare the codebase to listen to the Storefront changes (1/4)#1557
Conversation
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| lazy var storeKit2Listener = StoreKit2TransactionListener(delegate: self) | ||
| lazy var storeKit2TransactionListener = StoreKit2TransactionListener(delegate: self) |
There was a problem hiding this comment.
In the future, we could create a single listener that would be composed of other "sublisteners"
There was a problem hiding this comment.
Can this be private(set)?
There was a problem hiding this comment.
yep 🙂 let me change it Finally, it's not possible, we are setting this listener in the tests, so we need the set method as public 😕 Admittedly it is only needed for testing, but I would leave it there for now.
WDYT? 🤔
| import XCTest | ||
|
|
||
| @available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
| class StoreKit2StorefrontListenerTests: XCTestCase { |
There was a problem hiding this comment.
I tried to add a test case to cover when the Storefront changes, but I couldn’t. Is there is a way? 🤔
There was a problem hiding this comment.
Yeah I don't think we can change the storefront mid-way through a test run unfortunately.
| @available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
| class StoreKit2StorefrontListener { | ||
|
|
||
| private(set) var taskHandle: Task<Void, Never>? |
There was a problem hiding this comment.
I would implement didSet here like so:
didSet {
if self.taskHandle !== oldValue {
oldValue?.cancel()
}
}Then you only need to set it to nil or the new value.
There was a problem hiding this comment.
much better 👍🏼 Thanks :)
There was a problem hiding this comment.
Just a question, why do you have to check if they are identical, checking if they are not equal wouldn't be enough? 🤔
There was a problem hiding this comment.
Oh it's a struct. Yeah == is fine:
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| lazy var storeKit2Listener = StoreKit2TransactionListener(delegate: self) | ||
| lazy var storeKit2TransactionListener = StoreKit2TransactionListener(delegate: self) |
There was a problem hiding this comment.
Can this be private(set)?
| lazy var storeKit2TransactionListener = StoreKit2TransactionListener(delegate: self) | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| lazy var storeKit2StorefrontListener = StoreKit2StorefrontListener(delegate: self) |
| taskHandle?.cancel() | ||
| taskHandle = nil |
There was a problem hiding this comment.
This is duplicated, you can just do self.taskHandle = nil now :)
The advantage of what I suggested is that now it's impossible to have a reference to a cancelled task :)
There was a problem hiding this comment.
Yeah! good catch! I forgot to update this line 🤦🏼♂️
There was a problem hiding this comment.
I've reverted the change because when the listener is released, the didSet observer is not called 😅 so the task is not cancelled. I've cancelled the task when the listener is released and modified the test cases to cover this scenario.
There was a problem hiding this comment.
Oooh that's a great catch I forgot!!
| taskHandle = Task { [weak self] in | ||
| for await _ in StoreKit.Storefront.updates { | ||
| guard let self = self else { break } | ||
| self.delegate?.storefrontDidUpdate() |
There was a problem hiding this comment.
Maybe put this inside of MainActor.run {}?
| import XCTest | ||
|
|
||
| @available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) | ||
| class StoreKit2StorefrontListenerTests: XCTestCase { |
There was a problem hiding this comment.
Yeah I don't think we can change the storefront mid-way through a test run unfortunately.
| expect(handle).toNot(beNil()) | ||
| expect(handle?.isCancelled) == false | ||
|
|
||
| self.listener = nil | ||
| expect(handle?.isCancelled) == true |
Co-authored-by: NachoSoto <NachoSoto@users.noreply.github.com>
Co-authored-by: NachoSoto <NachoSoto@users.noreply.github.com>
Co-authored-by: NachoSoto <NachoSoto@users.noreply.github.com>
|
@NachoSoto Thanks for the review and feedback! ❤️ |
|
I'll wait to see if @aboedo has any feedback before merging this. |
|
Hi guys! 👋🏼 Did you have a chance to take a look at this PR? 🙂 The second PR is ready to be opened but I can't because I'm working on a fork and I can't select this branch as the base of the other PR. So, I have to wait for this PR to be merged to open the other one, otherwise, you would see the same changes 😅 |
Follow up to #1557. Subtask of #1382 ### Description This PR aims to clear the stuff in the cache when the Storefront changes. Changes: - Add `clearCachedOfferings` method to `DeviceCache`. - Add `clearCache` method to `ProductFetcherSK1`. - Make `internal` the `clearCache` method in `ProductsFetcherSK2`. - Handle the **Storefront** changes in the `PurchasesOrchestator`. - Add tests to cover some scenarios.
Subtask of #1382
Description
This PR is the first step to preparing the codebase to respond to Storefront changes to update some stuff that is in the cache.
Changes:
SKPaymentTransactionObserverprotocol using thepaymentQueueDidChangeStorefrontmethod.StoreKit2TransactionListener).PurchasesOrchestrator, adding the new delegate methods.