@@ -35,14 +35,15 @@ class OSPropertyOperationExecutor: OSOperationExecutor {
3535
3636 init ( ) {
3737 // Read unfinished deltas from cache, if any...
38+ // Note that we should only have deltas for the current user as old ones are flushed..
3839 if var deltaQueue = OneSignalUserDefaults . initShared ( ) . getSavedCodeableData ( forKey: OS_PROPERTIES_EXECUTOR_DELTA_QUEUE_KEY, defaultValue: [ ] ) as? [ OSDelta ] {
3940 // Hook each uncached Delta to the model in the store
4041 for (index, delta) in deltaQueue. enumerated ( ) . reversed ( ) {
4142 if let modelInStore = OneSignalUserManagerImpl . sharedInstance. propertiesModelStore. getModel ( modelId: delta. model. modelId) {
42- // The model exists in the properties model store, set it to be the Delta's model
43+ // 1. The model exists in the properties model store, set it to be the Delta's model
4344 delta. model = modelInStore
4445 } else {
45- // The model does not exist, drop this Delta
46+ // 2. The model does not exist, drop this Delta
4647 deltaQueue. remove ( at: index)
4748 }
4849 }
@@ -55,13 +56,18 @@ class OSPropertyOperationExecutor: OSOperationExecutor {
5556 if var updateRequestQueue = OneSignalUserDefaults . initShared ( ) . getSavedCodeableData ( forKey: OS_PROPERTIES_EXECUTOR_UPDATE_REQUEST_QUEUE_KEY, defaultValue: [ ] ) as? [ OSRequestUpdateProperties ] {
5657 // Hook each uncached Request to the model in the store
5758 for (index, request) in updateRequestQueue. enumerated ( ) . reversed ( ) {
58- if let propertiesModel = OneSignalUserManagerImpl . sharedInstance. propertiesModelStore. getModel ( modelId: request. modelToUpdate. modelId) ,
59- let identityModel = OneSignalUserManagerImpl . sharedInstance. identityModelStore. getModel ( modelId: request. identityModel. modelId) {
60- // The models exist in the stores, set it to be the Request's models
59+ // 0. Hook up the properties model if its the current user's so it can hydrate
60+ if let propertiesModel = OneSignalUserManagerImpl . sharedInstance. propertiesModelStore. getModel ( modelId: request. modelToUpdate. modelId) {
6161 request. modelToUpdate = propertiesModel
62+ }
63+ if let identityModel = OneSignalUserManagerImpl . sharedInstance. identityModelStore. getModel ( modelId: request. identityModel. modelId) {
64+ // 1. The identity model exist in the store, set it to be the Request's models
65+ request. identityModel = identityModel
66+ } else if let identityModel = OSUserExecutor . identityModels [ request. identityModel. modelId] {
67+ // 2. The model exists in the user executor
6268 request. identityModel = identityModel
6369 } else if !request. prepareForExecution ( ) {
64- // The models do not exist AND this request cannot be sent, drop this Request
70+ // 3. The identitymodel do not exist AND this request cannot be sent, drop this Request
6571 updateRequestQueue. remove ( at: index)
6672 }
6773 }
@@ -119,21 +125,28 @@ class OSPropertyOperationExecutor: OSOperationExecutor {
119125 }
120126
121127 func executeUpdatePropertiesRequest( _ request: OSRequestUpdateProperties ) {
128+ guard !request. sentToClient else {
129+ return
130+ }
122131 guard request. prepareForExecution ( ) else {
123132 return
124133 }
125- OneSignalLog . onesignalLog ( . LL_VERBOSE, message: " OSPropertyOperationExecutor: executeUpdatePropertiesRequest making request: \( request) " )
126- OneSignalClient . shared ( ) . execute ( request) { _ in
134+ request. sentToClient = true
127135
128- // On success, remove request from cache, and hydrate model
129- // TODO: Do we actually hydrate model though?
130- // For example, if app restarts and we read in operations between sending this off and getting the response
136+ OneSignalClient . shared ( ) . execute ( request) { _ in
137+ // On success, remove request from cache, and we do need to hydrate
138+ // TODO: We need to hydrate after all
131139 self . updateRequestQueue. removeAll ( where: { $0 == request} )
132140 OneSignalUserDefaults . initShared ( ) . saveCodeableData ( forKey: OS_PROPERTIES_EXECUTOR_UPDATE_REQUEST_QUEUE_KEY, withValue: self . updateRequestQueue)
133-
134141 } onFailure: { error in
135- self . updateRequestQueue. removeAll ( where: { $0 == request} )
136- OneSignalLog . onesignalLog ( . LL_ERROR, message: error. debugDescription)
142+ OneSignalLog . onesignalLog ( . LL_ERROR, message: " OSPropertyOperationExecutor update properties request failed with error: \( error. debugDescription) " )
143+ // TODO: Differentiate error cases
144+ // If the error is not retryable, remove from cache and queue
145+ if let nsError = error as? NSError ,
146+ nsError. code < 500 && nsError. code != 0 {
147+ self . updateRequestQueue. removeAll ( where: { $0 == request} )
148+ OneSignalUserDefaults . initShared ( ) . saveCodeableData ( forKey: OS_PROPERTIES_EXECUTOR_UPDATE_REQUEST_QUEUE_KEY, withValue: self . updateRequestQueue)
149+ }
137150 }
138151 }
139152}
0 commit comments