88
99import StoreKit
1010
11- /// This observable class can manage store-based information
12- /// in a way that makes it observable.
11+ /// This observable class can manage store-based state in an observable way.
1312///
14- /// You can use the ``StandardStoreService`` to sync product
15- /// info with this context, which will update the ``products``
16- /// array with matching products.
13+ /// You can use the ``StandardStoreService`` to sync product information
14+ /// with a context instance when performing certain operations.
1715///
18- /// Since StoreKit `Product` isn't `Codable`, the ``products``
19- /// array is not permanently persisted, which means that the
20- /// property will reset when the app is restarted.
21- ///
22- /// Due to this, there's also a ``productIds`` property that
23- /// is permanently persisted. It gives you the option to map
24- /// ``productIds`` to local product representations whenever
25- /// the app can't access StoreKit.
26- ///
27- /// However, a StoreKit `Product` value is needed to perform
28- /// a purchase. If your app fails to fetch products, e.g. if
29- /// it's offline, you should show a spinner or an alert that
30- /// informs your users that your products can't be retrieved.
16+ /// Since StoreKit `Product` isn't `Codable`, the ``products`` array isn't
17+ /// permanently persisted. This means that it will reset when the app is restarted.
18+ /// Due to this, there's also a ``productIds`` property that *is* permanently
19+ /// persisted. This gives you an option to map ``productIds`` to local product
20+ /// representations whenever the app can't access StoreKit. However, note that a
21+ /// StoreKit `Product` is needed to perform a purchase.
3122public class StoreContext : ObservableObject , @unchecked Sendable {
3223
3324 /// Create a context instance.
@@ -41,49 +32,23 @@ public class StoreContext: ObservableObject, @unchecked Sendable {
4132 }
4233
4334 /// A list of synced products.
44- ///
45- /// You can use this property to keep track of a product
46- /// collection that has been fetched from StoreKit.
47- ///
48- /// Since `Product` isn't `Codable`, this property can't
49- /// be persisted and must be reloaded on app launch.
5035 @Published
5136 public var products : [ Product ] {
5237 didSet { productIds = products. map { $0. id} }
5338 }
5439
55- /// A list of synced products IDs.
56- ///
57- /// You can use this property to keep track of a product
58- /// collection that has been fetched from StoreKit.
59- ///
60- /// This property is persisted, which means that you can
61- /// map these IDs to a local product representation when
62- /// a StoreKit request fails.
40+ /// A persisted list of synced products IDs.
6341 @Published
6442 public internal( set) var productIds : [ String ] = [ ] {
6543 willSet { persistedProductIds = newValue }
6644 }
6745
6846 /// A list of active purchase transactions.
69- ///
70- /// You can use this property to keep track of purchased
71- /// products that has been fetched from StoreKit.
72- ///
73- /// Since `Transaction` isn't `Codable` this property is
74- /// not persisted and must be reloaded on app launch.
7547 public var purchaseTransactions : [ Transaction ] = [ ] {
7648 didSet { purchasedProductIds = purchaseTransactions. map { $0. productID } }
7749 }
7850
7951 /// A list of purchased product IDs.
80- ///
81- /// You can use this property to keep track of purchased
82- /// products that has been fetched from StoreKit.
83- ///
84- /// This property is persisted, which means that you can
85- /// map these IDs to a local product representation when
86- /// a StoreKit request fails.
8752 @Published
8853 public internal( set) var purchasedProductIds : [ String ] = [ ] {
8954 willSet { persistedPurchasedProductIds = newValue }
0 commit comments