Skip to content

Commit df8c1a5

Browse files
committed
Merge branch 'woomob-619-xcode-warnings-performing-io-on-the-main-thread-can-cause' into woomob-619-product-list-update
2 parents 07f9d23 + f2fadad commit df8c1a5

File tree

3 files changed

+65
-76
lines changed

3 files changed

+65
-76
lines changed

Modules/Sources/Yosemite/Tools/ResultsController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public class ResultsController<T: ResultsControllerMutableType> {
194194
}
195195

196196
/// Returns an array of all of the (ReadOnly) Fetched Objects.
197+
/// Note: Avoid calling this in computed variables as the conversion of storage items can be costly.
197198
///
198199
public var fetchedObjects: [T.ReadOnlyType] {
199200
let readOnlyObjects = controller.fetchedObjects?.compactMap { mutableObject in
@@ -346,7 +347,8 @@ public extension ResultsController {
346347

347348

348349
public extension ResultsController where T: ListItemConvertible {
349-
/// Returns an array of all list items mapped from the fetched objects
350+
/// Returns an array of all list items mapped from the fetched objects.
351+
/// Note: Avoid calling this in computed variables as the conversion of storage items can be costly.
350352
///
351353
var listItemObjects: [T.ListItemType] {
352354
let listItemObjects = controller.fetchedObjects?.compactMap { mutableObject in

WooCommerce/Classes/ViewModels/Order Details/OrderDetailsResultsControllers.swift

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -88,72 +88,41 @@ final class OrderDetailsResultsControllers {
8888

8989
/// Order shipment tracking list
9090
///
91-
var orderTracking: [ShipmentTracking] {
92-
return trackingResultsController.fetchedObjects
93-
}
91+
private(set) var orderTracking: [ShipmentTracking] = []
9492

9593
/// Order statuses list
9694
///
97-
var currentSiteStatuses: [OrderStatus] {
98-
return statusResultsController.fetchedObjects
99-
}
95+
private(set) var currentSiteStatuses: [OrderStatus] = []
10096

10197
/// Products from an Order
10298
///
103-
var products: [ProductListItem] {
104-
productResultsController.listItemObjects
105-
}
99+
private(set) var products: [ProductListItem] = []
106100

107101
/// ProductVariations from an Order
108102
///
109-
var productVariations: [ProductVariation] {
110-
return productVariationResultsController.fetchedObjects
111-
}
103+
private(set) var productVariations: [ProductVariation] = []
112104

113105
/// Refunds in an Order
114106
///
115-
var refunds: [Refund] {
116-
return refundResultsController.fetchedObjects
117-
}
107+
private(set) var refunds: [Refund] = []
118108

119109
/// Shipping labels for an Order
120110
///
121-
var shippingLabels: [ShippingLabel] {
122-
guard shipments.isEmpty else {
123-
return shipments.compactMap { $0.shippingLabel }
124-
}
125-
return order.shippingLabels.sorted(by: { label1, label2 in
126-
if let shipmentID1 = label1.shipmentID,
127-
let shipmentID2 = label2.shipmentID {
128-
return shipmentID1.localizedStandardCompare(shipmentID2) == .orderedAscending
129-
}
130-
return label1.dateCreated < label2.dateCreated
131-
})
132-
}
111+
private(set) var shippingLabels: [ShippingLabel] = []
133112

134-
var shipments: [WooShippingShipment] {
135-
shipmentResultsController.fetchedObjects
136-
}
113+
private(set) var shipments: [WooShippingShipment] = []
137114

138115
/// Site's add-on groups.
139116
///
140-
var addOnGroups: [AddOnGroup] {
141-
return addOnGroupResultsController.fetchedObjects
142-
}
117+
private(set) var addOnGroups: [AddOnGroup] = []
143118

144-
var sitePlugins: [SitePlugin] {
145-
return sitePluginsResultsController.fetchedObjects
146-
}
119+
private(set) var sitePlugins: [SitePlugin] = []
147120

148-
var feeLines: [OrderFeeLine] {
149-
return order.fees
150-
}
121+
private(set) var feeLines: [OrderFeeLine] = []
151122

152123
/// Shipping methods list
153124
///
154-
var siteShippingMethods: [ShippingMethod] {
155-
return shippingMethodsResultsController.fetchedObjects
156-
}
125+
private(set) var siteShippingMethods: [ShippingMethod] = []
157126

158127
/// Completion handler for when results controllers reload.
159128
///
@@ -164,6 +133,8 @@ final class OrderDetailsResultsControllers {
164133
self.order = order
165134
self.siteID = order.siteID
166135
self.storageManager = storageManager
136+
feeLines = order.fees
137+
updateShippingLabels()
167138
}
168139

169140
func configureResultsControllers(onReload: @escaping () -> Void) {
@@ -181,6 +152,8 @@ final class OrderDetailsResultsControllers {
181152

182153
func update(order: Order) {
183154
self.order = order
155+
feeLines = order.fees
156+
updateShippingLabels()
184157
// Product variation results controller depends on order items to load variations,
185158
// so we need to recreate it whenever receiving an updated order.
186159
self.productVariationResultsController = getProductVariationResultsController()
@@ -202,7 +175,9 @@ private extension OrderDetailsResultsControllers {
202175
}
203176

204177
func configureShipmentResultsController(onReload: @escaping () -> Void) {
205-
shipmentResultsController.onDidChangeContent = {
178+
shipmentResultsController.onDidChangeContent = { [weak self] in
179+
guard let self else { return }
180+
shipments = shipmentResultsController.fetchedObjects
206181
onReload()
207182
}
208183

@@ -216,6 +191,7 @@ private extension OrderDetailsResultsControllers {
216191

217192
do {
218193
try shipmentResultsController.performFetch()
194+
shipments = shipmentResultsController.fetchedObjects
219195
} catch {
220196
DDLogError("⛔️ Unable to fetch shipments: \(error)")
221197
}
@@ -224,13 +200,16 @@ private extension OrderDetailsResultsControllers {
224200
func configureStatusResultsController() {
225201
do {
226202
try statusResultsController.performFetch()
203+
currentSiteStatuses = statusResultsController.fetchedObjects
227204
} catch {
228205
DDLogError("⛔️ Unable to fetch Order Statuses: \(error)")
229206
}
230207
}
231208

232209
private func configureTrackingResultsController(onReload: @escaping () -> Void) {
233-
trackingResultsController.onDidChangeContent = {
210+
trackingResultsController.onDidChangeContent = { [weak self] in
211+
guard let self else { return }
212+
orderTracking = trackingResultsController.fetchedObjects
234213
onReload()
235214
}
236215

@@ -244,13 +223,16 @@ private extension OrderDetailsResultsControllers {
244223

245224
do {
246225
try trackingResultsController.performFetch()
226+
orderTracking = trackingResultsController.fetchedObjects
247227
} catch {
248228
DDLogError("⛔️ Unable to fetch Order \(order.orderID) shipment tracking details: \(error)")
249229
}
250230
}
251231

252232
private func configureProductResultsController(onReload: @escaping () -> Void) {
253-
productResultsController.onDidChangeContent = {
233+
productResultsController.onDidChangeContent = { [weak self] in
234+
guard let self else { return }
235+
products = productResultsController.listItemObjects
254236
onReload()
255237
}
256238

@@ -264,13 +246,16 @@ private extension OrderDetailsResultsControllers {
264246

265247
do {
266248
try productResultsController.performFetch()
249+
products = productResultsController.listItemObjects
267250
} catch {
268251
DDLogError("⛔️ Unable to fetch Products for Site \(siteID): \(error)")
269252
}
270253
}
271254

272255
private func configureProductVariationResultsController(onReload: @escaping () -> Void) {
273-
productVariationResultsController.onDidChangeContent = {
256+
productVariationResultsController.onDidChangeContent = { [weak self] in
257+
guard let self else { return }
258+
productVariations = productVariationResultsController.fetchedObjects
274259
onReload()
275260
}
276261

@@ -284,13 +269,16 @@ private extension OrderDetailsResultsControllers {
284269

285270
do {
286271
try productVariationResultsController.performFetch()
272+
productVariations = productVariationResultsController.fetchedObjects
287273
} catch {
288274
DDLogError("⛔️ Error fetching ProductVariations for Order \(order.orderID): \(error)")
289275
}
290276
}
291277

292278
private func configureRefundResultsController(onReload: @escaping () -> Void) {
293-
refundResultsController.onDidChangeContent = {
279+
refundResultsController.onDidChangeContent = { [weak self] in
280+
guard let self else { return }
281+
refunds = refundResultsController.fetchedObjects
294282
onReload()
295283
}
296284

@@ -304,13 +292,16 @@ private extension OrderDetailsResultsControllers {
304292

305293
do {
306294
try refundResultsController.performFetch()
295+
refunds = refundResultsController.fetchedObjects
307296
} catch {
308297
DDLogError("⛔️ Unable to fetch Refunds for Site \(siteID) and Order \(order.orderID): \(error)")
309298
}
310299
}
311300

312301
private func configureAddOnGroupResultsController(onReload: @escaping () -> Void) {
313-
addOnGroupResultsController.onDidChangeContent = {
302+
addOnGroupResultsController.onDidChangeContent = { [weak self] in
303+
guard let self else { return }
304+
addOnGroups = addOnGroupResultsController.fetchedObjects
314305
onReload()
315306
}
316307

@@ -322,13 +313,16 @@ private extension OrderDetailsResultsControllers {
322313

323314
do {
324315
try addOnGroupResultsController.performFetch()
316+
addOnGroups = addOnGroupResultsController.fetchedObjects
325317
} catch {
326318
DDLogError("⛔️ Unable to fetch AddOnGroups for Site \(siteID): \(error)")
327319
}
328320
}
329321

330322
private func configureSitePluginsResultsController(onReload: @escaping () -> Void) {
331-
sitePluginsResultsController.onDidChangeContent = {
323+
sitePluginsResultsController.onDidChangeContent = { [weak self] in
324+
guard let self else { return }
325+
sitePlugins = sitePluginsResultsController.fetchedObjects
332326
onReload()
333327
}
334328

@@ -340,13 +334,16 @@ private extension OrderDetailsResultsControllers {
340334

341335
do {
342336
try sitePluginsResultsController.performFetch()
337+
sitePlugins = sitePluginsResultsController.fetchedObjects
343338
} catch {
344339
DDLogError("⛔️ Unable to fetch Site Plugins for Site \(siteID): \(error)")
345340
}
346341
}
347342

348343
private func configureShippingMethodsResultsController(onReload: @escaping () -> Void) {
349-
shippingMethodsResultsController.onDidChangeContent = {
344+
shippingMethodsResultsController.onDidChangeContent = { [weak self] in
345+
guard let self else { return }
346+
siteShippingMethods = shippingMethodsResultsController.fetchedObjects
350347
onReload()
351348
}
352349

@@ -358,6 +355,7 @@ private extension OrderDetailsResultsControllers {
358355

359356
do {
360357
try shippingMethodsResultsController.performFetch()
358+
siteShippingMethods = shippingMethodsResultsController.fetchedObjects
361359
} catch {
362360
DDLogError("⛔️ Unable to fetch Shipping Methods for Site \(siteID): \(error)")
363361
}
@@ -375,4 +373,18 @@ private extension OrderDetailsResultsControllers {
375373
try? sitePluginsResultsController.performFetch()
376374
try? shippingMethodsResultsController.performFetch()
377375
}
376+
377+
func updateShippingLabels() {
378+
guard shipments.isEmpty else {
379+
shippingLabels = shipments.compactMap { $0.shippingLabel }
380+
return
381+
}
382+
shippingLabels = order.shippingLabels.sorted(by: { label1, label2 in
383+
if let shipmentID1 = label1.shipmentID,
384+
let shipmentID2 = label2.shipmentID {
385+
return shipmentID1.localizedStandardCompare(shipmentID2) == .orderedAscending
386+
}
387+
return label1.dateCreated < label2.dateCreated
388+
})
389+
}
378390
}

WooCommerce/Classes/ViewModels/Order Details/Shipping Labels/AggregatedShippingLabelOrderItems.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,30 +145,6 @@ private extension AggregatedShippingLabelOrderItems {
145145
attributes: orderItem?.attributes ?? [],
146146
addOns: orderItem?.addOns ?? [],
147147
parent: orderItem?.parent)
148-
case .product(let product, let orderItem, let name):
149-
let itemID = orderItem?.itemID.description ?? "0"
150-
let productName = orderItem?.name ?? name
151-
let price = orderItem?.price ??
152-
currencyFormatter.convertToDecimal(product.price) ?? 0
153-
let totalPrice = price.multiplying(by: .init(decimal: Decimal(quantity)))
154-
let imageURL: URL?
155-
if let encodedImageURLString = product.images.first?.src.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
156-
imageURL = URL(string: encodedImageURLString)
157-
} else {
158-
imageURL = nil
159-
}
160-
return .init(itemID: itemID,
161-
productID: product.productID,
162-
variationID: 0,
163-
name: productName,
164-
price: price,
165-
quantity: Decimal(quantity),
166-
sku: orderItem?.sku ?? product.sku,
167-
total: totalPrice,
168-
imageURL: imageURL,
169-
attributes: orderItem?.attributes ?? [],
170-
addOns: orderItem?.addOns ?? [],
171-
parent: orderItem?.parent)
172148
case .productVariation(let variation, let orderItem, let name):
173149
let itemID = orderItem?.itemID.description ?? "0"
174150
let productName = orderItem?.name ?? name
@@ -215,7 +191,6 @@ private extension AggregatedShippingLabelOrderItems {
215191
/// The underlying model for an order item.
216192
enum OrderItemModel {
217193
case productName(name: String)
218-
case product(product: Product, orderItem: OrderItem?, name: String)
219194
case productVariation(productVariation: ProductVariation, orderItem: OrderItem?, name: String)
220195
case productListItem(item: ProductListItem, orderItem: OrderItem?, name: String)
221196
}

0 commit comments

Comments
 (0)