Skip to content

Commit a0176fd

Browse files
authored
add method for cleaning Leanplum_Resources cache folder on every new … (#353)
* add method for cleaning Leanplum_Resources cache folder on every new version of the sdk * changing the place where clearCacheIfNeeded method is called. Instead of calling it in start method now it is called in startWith.... method because this method is doing all the work for starting the sdk * changing the name of the method to be more understandable
1 parent 4ac4f0b commit a0176fd

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Leanplum-SDK/Classes/Internal/Leanplum.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ + (void)startWithUserId:(NSString *)userId
742742
return;
743743
}
744744

745+
[LPFileManager clearCacheIfSDKUpdated];
746+
745747
[[LPCountAggregator sharedAggregator] incrementCount:@"start_with_user_id"];
746748

747749
LP_TRY

Leanplum-SDK/Classes/Managers/LPFileManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@
8181
* Adds an attribute to a file at filepath to exclude it from iCloud and iTunes backup.
8282
*/
8383
+ (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *__nonnull) filePathString;
84-
84+
+ (void)clearCacheIfSDKUpdated;
8585
@end

Leanplum-SDK/Classes/Managers/LPFileManager.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,4 +856,27 @@ + (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
856856
LP_END_TRY
857857
}
858858

859+
+ (void)clearCacheIfSDKUpdated {
860+
NSString *savedSdkVersionKey = @"savedSdkVersion";
861+
NSString *savedSdkVersion = [[NSUserDefaults standardUserDefaults] valueForKey:savedSdkVersionKey];
862+
if(![[LPConstantsState sharedState].sdkVersion isEqualToString:savedSdkVersion]) {
863+
[[NSUserDefaults standardUserDefaults] setValue:[LPConstantsState sharedState].sdkVersion forKey:savedSdkVersionKey];
864+
[[NSUserDefaults standardUserDefaults] synchronize];
865+
NSString *cachesDir = [[self cachesDirectory] stringByAppendingPathComponent:@"Leanplum_Resources"];
866+
867+
NSFileManager *fileManager = [[NSFileManager alloc] init];
868+
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:cachesDir];
869+
NSString *file;
870+
871+
while (file = [enumerator nextObject]) {
872+
NSError *error = nil;
873+
BOOL result = [fileManager removeItemAtPath:[cachesDir stringByAppendingPathComponent:file] error:&error];
874+
875+
if (!result && error) {
876+
NSLog(@"Cleaning cache error: %@, for file %@", error, file);
877+
}
878+
}
879+
}
880+
}
881+
859882
@end

0 commit comments

Comments
 (0)