Skip to content

Commit 9b29ae1

Browse files
committed
Implement RCTStorageDirectoryMigrationCheck
1 parent f91c278 commit 9b29ae1

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

ios/RNCAsyncStorage.m

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray<NSDictionary *> *
7373
#else
7474
storageDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
7575
#endif
76-
storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent:RCTStorageDirectory];
76+
storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent:storageDir];
7777
return storageDirectoryPath;
7878
}
7979

@@ -162,10 +162,60 @@ static dispatch_queue_t RCTGetMethodQueue()
162162
return error ? RCTMakeError(@"Failed to delete storage directory.", error, nil) : nil;
163163
}
164164

165-
static void RCTPerformDirectoryMigrationCheck() {
166-
// NSString *oldRCTStorageDirectory = @"RNCAsyncLocalStorage_V1";
167-
// NSString *storageDir = RCTCreateStorageDirectoryPath(oldRCTStorageDirectory);
168-
// printf("%s%s\n", "TESTING: ", [storageDir UTF8String]);
165+
/**
166+
* This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data.
167+
* Data is migrated from the "RNCAsyncLocalStorage_V1" directory to the "RCTAsyncLocalStorage_V1" directory.
168+
*/
169+
static void RCTStorageDirectoryMigrationCheck() {
170+
NSString *oldStorageDir = RCTCreateStorageDirectoryPath(@"RNCAsyncLocalStorage_V1");
171+
172+
BOOL isDir;
173+
BOOL oldExists = [[NSFileManager defaultManager] fileExistsAtPath:oldStorageDir isDirectory:&isDir];
174+
175+
// If the old directory exists, it means we need to migrate data to the new directory
176+
if (oldExists && isDir) {
177+
NSError *error;
178+
BOOL migrationSuccess;
179+
// If the new storage directory already exists, then this may be caused by ever older data being left behind from previous versions.
180+
// This old data will need to be overwritten.
181+
NSString *newStorageDir = RCTGetStorageDirectory();
182+
BOOL newExists = [[NSFileManager defaultManager] fileExistsAtPath:newStorageDir isDirectory:&isDir];
183+
if (newExists && isDir) {
184+
NSString *backupStorageDir = RCTCreateStorageDirectoryPath(@"RCTBackupAsyncLocalStorage_V1");
185+
// Replace any possible existing data in the new storage with old storage directory
186+
migrationSuccess = [[NSFileManager defaultManager] replaceItemAtURL:[[NSURL alloc] initWithString:newStorageDir]
187+
withItemAtURL:[[NSURL alloc] initWithString:oldStorageDir]
188+
backupItemName:backupStorageDir
189+
options:NSFileManagerItemReplacementUsingNewMetadataOnly
190+
resultingItemURL:nil
191+
error:&error];
192+
if (error || !success) {
193+
// Attempt to recover from failed overwriting
194+
[[NSFileManager defaultManager] removeItemAtPath:newStorageDir error:nil];
195+
[[NSFileManager defaultManager] copyItemAtPath:backupStorageDir toPath:newStorageDir error:nil];
196+
[[NSFileManager defaultManager] removeItemAtPath:backupStorageDir error:nil];
197+
if (error) {
198+
RCTMakeError(@"Failed to overwrite old storage directory to new storage directory location during migration", error, nil);
199+
return;
200+
}
201+
}
202+
} else {
203+
// Copy over the old storage directory to new storage directory path
204+
migrationSuccess = [[NSFileManager defaultManager] copyItemAtPath:oldStorageDir toPath:newStorageDir error:&error];
205+
if (error) {
206+
RCTMakeError(@"Failed to copy old storage directory to new storage directory location during migration", error, nil);
207+
return;
208+
}
209+
}
210+
211+
// If the migration was a success, remove old storage directory
212+
if (migrationSuccess) {
213+
[[NSFileManager defaultManager] removeItemAtPath:oldStorageDir error:error];
214+
if (error) {
215+
RCTMakeError(@"Failed to remove old storage directory after migration", error, nil);
216+
}
217+
}
218+
}
169219
}
170220

171221
#pragma mark - RNCAsyncStorage
@@ -188,7 +238,7 @@ - (instancetype)init
188238
if (!(self = [super init])) {
189239
return nil;
190240
}
191-
RCTPerformDirectoryMigrationCheck();
241+
RCTStorageDirectoryMigrationCheck();
192242
return self;
193243
}
194244

0 commit comments

Comments
 (0)