Skip to content

Commit a761e14

Browse files
committed
Replace computed variables for fetched objects
1 parent 52cc176 commit a761e14

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

WooCommerce/Classes/ViewRelated/Blaze/CampaignCreation/BlazeCampaignCreationFormViewModel.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,7 @@ final class BlazeCampaignCreationFormViewModel: ObservableObject {
203203
@Published private(set) var isUsingAISuggestions: Bool = false
204204

205205
private let storage: StorageManagerType
206-
private var product: ProductListItem? {
207-
guard let product = productsResultsController.listItemObjects.first else {
208-
assertionFailure("Unable to fetch product with ID: \(productID)")
209-
return nil
210-
}
211-
return product
212-
}
206+
private var product: ProductListItem?
213207

214208
@Published private(set) var error: BlazeCampaignCreationError?
215209
private var suggestions: [BlazeAISuggestion] = []
@@ -306,6 +300,8 @@ final class BlazeCampaignCreationFormViewModel: ObservableObject {
306300
// sets isEvergreen = true by default if evergreen campaigns are supported
307301
self.isEvergreen = featureFlagService.isFeatureFlagEnabled(.blazeEvergreenCampaigns)
308302

303+
product = productsResultsController.listItemObjects.first
304+
309305
initializeCampaignObjective()
310306
updateBudgetDetails()
311307
updateTargetLanguagesText()

WooCommerce/Classes/ViewRelated/Dashboard/Blaze/BlazeCampaignDashboardViewModel.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ final class BlazeCampaignDashboardViewModel: ObservableObject {
100100
sortOrder: .dateDescending)
101101
}()
102102

103-
var latestPublishedProduct: ProductListItem? {
104-
productResultsController.listItemObjects.first
105-
}
103+
private(set) var latestPublishedProduct: ProductListItem?
106104

107105
private var subscriptions: Set<AnyCancellable> = []
108106

@@ -315,8 +313,10 @@ private extension BlazeCampaignDashboardViewModel {
315313
}
316314

317315
productResultsController.onDidChangeContent = { [weak self] in
318-
self?.updateAvailability()
319-
self?.updateResults()
316+
guard let self else { return }
317+
latestPublishedProduct = productResultsController.listItemObjects.first
318+
updateAvailability()
319+
updateResults()
320320
}
321321
productResultsController.onDidResetContent = { [weak self] in
322322
self?.updateAvailability()
@@ -326,6 +326,7 @@ private extension BlazeCampaignDashboardViewModel {
326326
do {
327327
try blazeCampaignResultsController.performFetch()
328328
try productResultsController.performFetch()
329+
latestPublishedProduct = productResultsController.listItemObjects.first
329330
updateResults()
330331
} catch {
331332
ServiceLocator.crashLogging.logError(error)

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Items Section/WooShippingItemsDataSource.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ final class DefaultWooShippingItemsDataSource: WooShippingItemsDataSource {
3030

3131
/// Stored products that match the items in the order.
3232
///
33-
private var products: [ProductListItem] {
34-
try? productResultsController.performFetch()
35-
return productResultsController.listItemObjects
36-
}
33+
private var products: [ProductListItem] = []
3734

3835
/// Stored product variations that match the items in the order.
3936
///
@@ -79,6 +76,7 @@ final class DefaultWooShippingItemsDataSource: WooShippingItemsDataSource {
7976
private func configureProductResultsController() {
8077
do {
8178
try productResultsController.performFetch()
79+
products = productResultsController.listItemObjects
8280
} catch {
8381
DDLogError("⛔️ Error fetching products for Woo Shipping label creation: \(error)")
8482
}

0 commit comments

Comments
 (0)