@@ -215,12 +215,32 @@ static void RCTStorageDirectoryCleanupOld(NSString *oldDirectoryPath)
215215 }
216216}
217217
218+ static void _createStorageDirectory (NSString *storageDirectory, NSError **error)
219+ {
220+ [[NSFileManager defaultManager ] createDirectoryAtPath: storageDirectory
221+ withIntermediateDirectories: YES
222+ attributes: nil
223+ error: error];
224+ }
225+
218226static void RCTStorageDirectoryMigrate (NSString *oldDirectoryPath, NSString *newDirectoryPath, BOOL shouldCleanupOldDirectory)
219227{
220228 NSError *error;
221229 // Migrate data by copying old storage directory to new storage directory location
222230 if (![[NSFileManager defaultManager ] copyItemAtPath: oldDirectoryPath toPath: newDirectoryPath error: &error]) {
223- RCTStorageDirectoryMigrationLogError (@" Failed to copy old storage directory to new storage directory location during migration" , error);
231+ // the new storage directory "Application Support/[bundleID]/RCTAsyncLocalStorage_V1" seems unable to migrate
232+ // because folder "Application Support/[bundleID]" doesn't exist.. create this folder and attempt folder copying again
233+ if (error != nil && error.code == 4 && [newDirectoryPath isEqualToString: RCTGetStorageDirectory ()]) {
234+ NSError *error = nil ;
235+ _createStorageDirectory (RCTCreateStorageDirectoryPath (@" " ), &error);
236+ if (error == nil ) {
237+ RCTStorageDirectoryMigrate (oldDirectoryPath, newDirectoryPath, shouldCleanupOldDirectory);
238+ } else {
239+ RCTStorageDirectoryMigrationLogError (@" Failed to create storage directory during migration." , error);
240+ }
241+ } else {
242+ RCTStorageDirectoryMigrationLogError (@" Failed to copy old storage directory to new storage directory location during migration" , error);
243+ }
224244 } else if (shouldCleanupOldDirectory) {
225245 // If copying succeeds, remove old storage directory
226246 RCTStorageDirectoryCleanupOld (oldDirectoryPath);
@@ -356,10 +376,7 @@ - (NSDictionary *)_ensureSetup
356376
357377 NSError *error = nil ;
358378 if (!RCTHasCreatedStorageDirectory) {
359- [[NSFileManager defaultManager ] createDirectoryAtPath: RCTGetStorageDirectory ()
360- withIntermediateDirectories: YES
361- attributes: nil
362- error: &error];
379+ _createStorageDirectory (RCTGetStorageDirectory (), &error);
363380 if (error) {
364381 return RCTMakeError (@" Failed to create storage directory." , error, nil );
365382 }
0 commit comments