@@ -116,16 +116,66 @@ - (instancetype)init
116116
117117- (void )retrieveServerConfigFromStorage : (NSString *)sdkBehaviorSettings
118118{
119- NSError *error = nil ;
120- NSDictionary *serverConfigObject = [CountlyPersistency.sharedInstance retrieveServerConfig ];
121- if (serverConfigObject.count == 0 && sdkBehaviorSettings)
119+ NSMutableDictionary *persistentBehaviorSettings = [CountlyPersistency.sharedInstance retrieveServerConfig ];
120+ if (persistentBehaviorSettings.count == 0 && sdkBehaviorSettings)
122121 {
123- serverConfigObject = [NSJSONSerialization JSONObjectWithData: [sdkBehaviorSettings cly_dataUTF8 ] options: 0 error: &error];
122+ NSError *error = nil ;
123+ id parsed = [NSJSONSerialization JSONObjectWithData: [sdkBehaviorSettings cly_dataUTF8 ] options: 0 error: &error];
124+
125+ if ([parsed isKindOfClass: [NSDictionary class ]]) {
126+ persistentBehaviorSettings = [(NSDictionary *)parsed mutableCopy ];
127+ [CountlyPersistency.sharedInstance storeServerConfig: persistentBehaviorSettings];
128+ } else {
129+ CLY_LOG_W (@" %s , Failed to parse sdkBehaviorSettings or not a dictionary: %@ " , __FUNCTION__, error);
130+ }
124131 }
125132
126- if (serverConfigObject.count > 0 && !error)
133+ [self populateServerConfig: persistentBehaviorSettings];
134+ }
135+
136+ - (void )mergeBehaviorSettings : (NSMutableDictionary *)baseConfig
137+ withConfig : (NSDictionary *)newConfig
138+ {
139+ // c, t and v paramters must exist
140+ if (newConfig.count != 3 || !newConfig[kRConfig ]) {
141+ CLY_LOG_D (@" %s , missing entries for a behavior settings omitting" , __FUNCTION__);
142+ return ;
143+ }
144+
145+ if (!newConfig[kRVersion ] || !newConfig[kRTimestamp ])
127146 {
128- [self populateServerConfig: serverConfigObject];
147+ CLY_LOG_D (@" %s , version or timestamp is missing in the behavioır settings omitting" , __FUNCTION__);
148+ return ;
149+ }
150+
151+ if (!([newConfig[kRConfig ] isKindOfClass: [NSDictionary class ]]) || ((NSDictionary *)newConfig[kRConfig ]).count == 0 ){
152+ CLY_LOG_D (@" %s , invalid behavior settings omitting" , __FUNCTION__);
153+ return ;
154+ }
155+
156+ id timestamp = newConfig[kRTimestamp ];
157+ if (timestamp) {
158+ baseConfig[kRTimestamp ] = timestamp;
159+ }
160+
161+ id version = newConfig[kRVersion ];
162+ if (version) {
163+ baseConfig[kRVersion ] = version;
164+ }
165+
166+ NSDictionary *cBase = baseConfig[kRConfig ] ?: NSMutableDictionary .new ;
167+ NSDictionary *cNew = newConfig[kRConfig ];
168+
169+ if ([cBase isKindOfClass: [NSDictionary class ]] || [cNew isKindOfClass: [NSDictionary class ]]) {
170+ NSMutableDictionary *cMerged = [cBase mutableCopy ];
171+
172+ [cNew enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
173+ if (obj != nil && obj != [NSNull null ]) {
174+ cMerged[key] = obj;
175+ }
176+ }];
177+
178+ baseConfig[kRConfig ] = cMerged;
129179 }
130180}
131181
@@ -379,9 +429,10 @@ - (void)fetchServerConfig:(CountlyConfig *)config
379429
380430 if (serverConfigResponse[kRConfig ] != nil )
381431 {
382- [CountlyPersistency.sharedInstance storeServerConfig: serverConfigResponse];
383- [self setDefaultValues ];
384- [self populateServerConfig: serverConfigResponse];
432+ NSMutableDictionary *persistentBehaviorSettings = [CountlyPersistency.sharedInstance retrieveServerConfig ];
433+ [self mergeBehaviorSettings: persistentBehaviorSettings withConfig: serverConfigResponse];
434+ [CountlyPersistency.sharedInstance storeServerConfig: persistentBehaviorSettings];
435+ [self populateServerConfig: persistentBehaviorSettings];
385436 }
386437
387438 [self notifySdkConfigChange: config]; // if no config let stored ones to be set
0 commit comments