@@ -85,7 +85,7 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray<NSDictionary *> *
85
85
#else
86
86
storageDirectoryPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ).firstObject ;
87
87
#endif
88
- storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent: RCTStorageDirectory ];
88
+ storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent: storageDir ];
89
89
return storageDirectoryPath;
90
90
}
91
91
@@ -174,10 +174,60 @@ static dispatch_queue_t RCTGetMethodQueue()
174
174
return error ? RCTMakeError (@" Failed to delete storage directory." , error, nil ) : nil ;
175
175
}
176
176
177
- static void RCTPerformDirectoryMigrationCheck () {
178
- // NSString *oldRCTStorageDirectory = @"RNCAsyncLocalStorage_V1";
179
- // NSString *storageDir = RCTCreateStorageDirectoryPath(oldRCTStorageDirectory);
180
- // printf("%s%s\n", "TESTING: ", [storageDir UTF8String]);
177
+ /* *
178
+ * This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data.
179
+ * Data is migrated from the "RNCAsyncLocalStorage_V1" directory to the "RCTAsyncLocalStorage_V1" directory.
180
+ */
181
+ static void RCTStorageDirectoryMigrationCheck () {
182
+ NSString *oldStorageDir = RCTCreateStorageDirectoryPath (@" RNCAsyncLocalStorage_V1" );
183
+
184
+ BOOL isDir;
185
+ BOOL oldExists = [[NSFileManager defaultManager ] fileExistsAtPath: oldStorageDir isDirectory: &isDir];
186
+
187
+ // If the old directory exists, it means we need to migrate data to the new directory
188
+ if (oldExists && isDir) {
189
+ NSError *error;
190
+ BOOL migrationSuccess;
191
+ // If the new storage directory already exists, then this may be caused by ever older data being left behind from previous versions.
192
+ // This old data will need to be overwritten.
193
+ NSString *newStorageDir = RCTGetStorageDirectory ();
194
+ BOOL newExists = [[NSFileManager defaultManager ] fileExistsAtPath: newStorageDir isDirectory: &isDir];
195
+ if (newExists && isDir) {
196
+ NSString *backupStorageDir = RCTCreateStorageDirectoryPath (@" RCTBackupAsyncLocalStorage_V1" );
197
+ // Replace any possible existing data in the new storage with old storage directory
198
+ migrationSuccess = [[NSFileManager defaultManager ] replaceItemAtURL: [[NSURL alloc ] initWithString: newStorageDir]
199
+ withItemAtURL: [[NSURL alloc ] initWithString: oldStorageDir]
200
+ backupItemName: backupStorageDir
201
+ options: NSFileManagerItemReplacementUsingNewMetadataOnly
202
+ resultingItemURL: nil
203
+ error: &error];
204
+ if (error || !success) {
205
+ // Attempt to recover from failed overwriting
206
+ [[NSFileManager defaultManager ] removeItemAtPath: newStorageDir error: nil ];
207
+ [[NSFileManager defaultManager ] copyItemAtPath: backupStorageDir toPath: newStorageDir error: nil ];
208
+ [[NSFileManager defaultManager ] removeItemAtPath: backupStorageDir error: nil ];
209
+ if (error) {
210
+ RCTMakeError (@" Failed to overwrite old storage directory to new storage directory location during migration" , error, nil );
211
+ return ;
212
+ }
213
+ }
214
+ } else {
215
+ // Copy over the old storage directory to new storage directory path
216
+ migrationSuccess = [[NSFileManager defaultManager ] copyItemAtPath: oldStorageDir toPath: newStorageDir error: &error];
217
+ if (error) {
218
+ RCTMakeError (@" Failed to copy old storage directory to new storage directory location during migration" , error, nil );
219
+ return ;
220
+ }
221
+ }
222
+
223
+ // If the migration was a success, remove old storage directory
224
+ if (migrationSuccess) {
225
+ [[NSFileManager defaultManager ] removeItemAtPath: oldStorageDir error: error];
226
+ if (error) {
227
+ RCTMakeError (@" Failed to remove old storage directory after migration" , error, nil );
228
+ }
229
+ }
230
+ }
181
231
}
182
232
183
233
#pragma mark - RNCAsyncStorage
@@ -200,7 +250,7 @@ - (instancetype)init
200
250
if (!(self = [super init ])) {
201
251
return nil ;
202
252
}
203
- RCTPerformDirectoryMigrationCheck ();
253
+ RCTStorageDirectoryMigrationCheck ();
204
254
return self;
205
255
}
206
256
0 commit comments