|
16 | 16 |
|
17 | 17 | static NSString *const RCTStorageDirectory = @"RCTAsyncLocalStorage_V1";
|
18 | 18 | static NSString *const RCTOldStorageDirectory = @"RNCAsyncLocalStorage_V1";
|
| 19 | +static NSString *const RCTExpoStorageDirectory = @"RCTAsyncLocalStorage"; |
19 | 20 | static NSString *const RCTManifestFileName = @"manifest.json";
|
20 | 21 | static const NSUInteger RCTInlineValueThreshold = 1024;
|
21 | 22 |
|
@@ -315,6 +316,46 @@ static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath,
|
315 | 316 | }
|
316 | 317 | }
|
317 | 318 |
|
| 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 | + |
318 | 359 | /**
|
319 | 360 | * This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data.
|
320 | 361 | * Check that data is migrated from the old location to the new location
|
@@ -385,15 +426,20 @@ - (instancetype)init
|
385 | 426 | return nil;
|
386 | 427 | }
|
387 | 428 |
|
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 | + } |
394 | 440 |
|
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" |
397 | 443 | RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory),
|
398 | 444 | RCTCreateStorageDirectoryPath(RCTStorageDirectory),
|
399 | 445 | NO);
|
|
0 commit comments