@@ -114,13 +114,13 @@ public final class CustomerStore: Store {
114114 with keyword: String ,
115115 with searchResults: [ WCAnalyticsCustomer ] ,
116116 onCompletion: @escaping ( Result < [ Customer ] , Error > ) -> Void ) {
117- var results = [ Customer] ( )
117+ var customers = [ Customer] ( )
118118 let group = DispatchGroup ( )
119119 for result in searchResults {
120120 group. enter ( )
121121 self . retrieveCustomer ( for: siteID, with: result. userID, onCompletion: { result in
122122 if let customer = try ? result. get ( ) {
123- results . append ( customer)
123+ customers . append ( customer)
124124 }
125125 group. leave ( )
126126 } )
@@ -130,13 +130,12 @@ public final class CustomerStore: Store {
130130 self . upsertSearchCustomerResult (
131131 siteID: siteID,
132132 keyword: keyword,
133- readOnlySearchResults : searchResults ,
133+ readOnlyCustomers : customers ,
134134 in: self . sharedDerivedStorage,
135135 onCompletion: {
136- onCompletion ( . success( results ) )
136+ onCompletion ( . success( customers ) )
137137 }
138138 )
139- //onCompletion(.success(results))
140139 }
141140 }
142141}
@@ -147,23 +146,22 @@ private extension CustomerStore {
147146 ///
148147 private func upsertSearchCustomerResult( siteID: Int64 ,
149148 keyword: String ,
150- readOnlySearchResults : [ Networking . WCAnalyticsCustomer ] ,
149+ readOnlyCustomers : [ Networking . Customer ] ,
151150 in storage: StorageType ,
152151 onCompletion: @escaping ( ) -> Void ) {
153-
154152 storage. perform {
155- // Potential stale data, perform search every time -> override existing? So we don't insert for each search.
156- let searchResult = storage. loadCustomerSearchResult ( siteID: siteID, keyword: keyword) ??
153+ let storedSeachResult = storage. loadCustomerSearchResult ( siteID: siteID, keyword: keyword) ??
157154 storage. insertNewObject ( ofType: Storage . CustomerSearchResult. self)
158- searchResult. keyword = keyword
159- for customer in readOnlySearchResults {
160- guard let storedCustomer = storage. loadCustomer ( siteID: siteID, customerID: customer. userID) else {
161- continue
155+
156+ storedSeachResult. siteID = siteID
157+ storedSeachResult. keyword = keyword
158+
159+ for result in readOnlyCustomers {
160+ if let storedCustomer = storage. loadCustomer ( siteID: siteID, customerID: result. customerID) {
161+ storedSeachResult. addToCustomers ( storedCustomer)
162162 }
163- storedCustomer. addToSearchResults ( searchResult)
164163 }
165164 }
166-
167165 storageManager. saveDerivedType ( derivedStorage: storage) {
168166 DispatchQueue . main. async ( execute: onCompletion)
169167 }
@@ -174,18 +172,15 @@ private extension CustomerStore {
174172 private func upsertCustomer( siteID: Int64 , readOnlyCustomer: Networking . Customer , in storage: StorageType , onCompletion: @escaping ( ) -> Void ) {
175173
176174 storage. perform {
177- // If the specific customerID for that siteID already exists, return it
178- // If doesn't, insert a new one in Storage
179175 let storageCustomer : Storage . Customer = {
176+ // If the specific customerID for that siteID already exists, return it
177+ // If doesn't, insert a new one in Storage
180178 if let storedCustomer = storage. loadCustomer ( siteID: siteID, customerID: readOnlyCustomer. customerID) {
181- print ( " Customer exists on Storage. ID: \( readOnlyCustomer. customerID) " )
182179 return storedCustomer
183180 } else {
184- print ( " Customer does not exist on Storage. ID: \( readOnlyCustomer. customerID) . Inserting new " )
185181 return storage. insertNewObject ( ofType: Storage . Customer. self)
186182 }
187183 } ( )
188- // 3. Update the entity
189184 storageCustomer. update ( with: readOnlyCustomer)
190185 }
191186
0 commit comments