Skip to content

Commit 8f19c1b

Browse files
authored
Migrate Expo storage directory on iOS (#563)
1 parent a21cb9f commit 8f19c1b

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

ios/RNCAsyncStorage.m

+54-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
static NSString *const RCTStorageDirectory = @"RCTAsyncLocalStorage_V1";
1818
static NSString *const RCTOldStorageDirectory = @"RNCAsyncLocalStorage_V1";
19+
static NSString *const RCTExpoStorageDirectory = @"RCTAsyncLocalStorage";
1920
static NSString *const RCTManifestFileName = @"manifest.json";
2021
static const NSUInteger RCTInlineValueThreshold = 1024;
2122

@@ -315,6 +316,46 @@ static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath,
315316
}
316317
}
317318

319+
320+
/**
321+
* Determine which of RCTOldStorageDirectory or RCTExpoStorageDirectory needs to migrated.
322+
* If both exist, we remove the least recently modified and return the most recently modified.
323+
* Otherwise, this will return the path to whichever directory exists.
324+
* If no directory exists, then return nil.
325+
*/
326+
static NSString *RCTGetStoragePathForMigration()
327+
{
328+
BOOL isDir;
329+
NSString *oldStoragePath = RCTCreateStorageDirectoryPath_deprecated(RCTOldStorageDirectory);
330+
NSString *expoStoragePath = RCTCreateStorageDirectoryPath_deprecated(RCTExpoStorageDirectory);
331+
NSFileManager *fileManager = [NSFileManager defaultManager];
332+
BOOL oldStorageDirectoryExists = [fileManager fileExistsAtPath:oldStoragePath isDirectory:&isDir] && isDir;
333+
BOOL expoStorageDirectoryExists = [fileManager fileExistsAtPath:expoStoragePath isDirectory:&isDir] && isDir;
334+
335+
336+
// Check if both the old storage directory and Expo storage directory exist
337+
if (oldStorageDirectoryExists && expoStorageDirectoryExists) {
338+
// If the old storage has been modified more recently than Expo storage, then clear Expo storage.
339+
// Otherwise, clear the old storage.
340+
if ([RCTManifestModificationDate(RCTCreateManifestFilePath(oldStoragePath))
341+
compare:RCTManifestModificationDate(
342+
RCTCreateManifestFilePath(expoStoragePath))] == NSOrderedDescending) {
343+
RCTStorageDirectoryCleanupOld(expoStoragePath);
344+
return oldStoragePath;
345+
} else {
346+
RCTStorageDirectoryCleanupOld(oldStoragePath);
347+
return expoStoragePath;
348+
}
349+
} else if (oldStorageDirectoryExists) {
350+
return oldStoragePath;
351+
} else if (expoStorageDirectoryExists) {
352+
return expoStoragePath;
353+
} else {
354+
return nil;
355+
}
356+
}
357+
358+
318359
/**
319360
* This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data.
320361
* Check that data is migrated from the old location to the new location
@@ -385,15 +426,20 @@ - (instancetype)init
385426
return nil;
386427
}
387428

388-
// First migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" to
389-
// "Documents/.../RCTAsyncLocalStorage_V1"
390-
RCTStorageDirectoryMigrationCheck(
391-
RCTCreateStorageDirectoryPath_deprecated(RCTOldStorageDirectory),
392-
RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory),
393-
YES);
429+
// Get the path to any old storage directory that needs to be migrated. If multiple exist,
430+
// the oldest are removed and the most recently modified is returned.
431+
NSString *oldStoragePath = RCTGetStoragePathForMigration();
432+
if (oldStoragePath != nil) {
433+
// Migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" or
434+
// "Documents/.../RCTAsyncLocalStorage" to "Documents/.../RCTAsyncLocalStorage_V1"
435+
RCTStorageDirectoryMigrationCheck(
436+
oldStoragePath,
437+
RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory),
438+
YES);
439+
}
394440

395-
// Then migrate what's in "Documents/.../RCTAsyncLocalStorage_V1" to "Application
396-
// Support/[bundleID]/RCTAsyncLocalStorage_V1"
441+
// Migrate what's in "Documents/.../RCTAsyncLocalStorage_V1" to
442+
// "Application Support/[bundleID]/RCTAsyncLocalStorage_V1"
397443
RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory),
398444
RCTCreateStorageDirectoryPath(RCTStorageDirectory),
399445
NO);

0 commit comments

Comments
 (0)