@@ -39,32 +39,30 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
3939 guard let storage = storage else {
4040 return [ ]
4141 }
42- // #2 Remote
42+
4343 do {
4444 let remoteCoupons = try await couponsRemote. loadAllCoupons ( for: siteID)
4545 return mapCouponsToPOSItems ( coupons: remoteCoupons)
4646 } catch {
47- debugPrint ( error)
48- return [ ]
49- }
47+ debugPrint ( " Failed to load coupons from remote: " , error)
5048
51- // #1 Storage
52- // let predicate = NSPredicate(format: "siteID == %lld", siteID)
53- // let descriptor = NSSortDescriptor(keyPath: \StorageCoupon.dateCreated,
54- // ascending: false)
55- //
56- // let resultsController = ResultsController<StorageCoupon>(storageManager: storage,
57- // matching: predicate,
58- // sortedBy: [descriptor])
59- //
60- // do {
61- // try resultsController.performFetch()
62- // let storeCoupons = resultsController.fetchedObjects
63- // return mapCouponsToPOSItems(coupons: storeCoupons)
64- // } catch {
65- // debugPrint(error)
66- // return []
67- // }
49+ // Fetch from storage as remote fallback
50+ let predicate = NSPredicate ( format: " siteID == %lld " , siteID)
51+ let descriptor = NSSortDescriptor ( keyPath: \StorageCoupon . dateCreated,
52+ ascending: false )
53+
54+ let resultsController = ResultsController < StorageCoupon > ( storageManager: storage,
55+ matching: predicate,
56+ sortedBy: [ descriptor] )
57+ do {
58+ try resultsController . performFetch ( )
59+ let storeCoupons = resultsController. fetchedObjects
60+ return mapCouponsToPOSItems ( coupons : storeCoupons )
61+ } catch {
62+ debugPrint ( " Failed to load coupons from storage: " , error )
63+ return [ ]
64+ }
65+ }
6866 }
6967
7068 private func mapCouponsToPOSItems( coupons: [ Coupon ] ) -> [ POSItem ] {
0 commit comments