@@ -73,7 +73,7 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray<NSDictionary *> *
73
73
#else
74
74
storageDirectoryPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ).firstObject ;
75
75
#endif
76
- storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent: RCTStorageDirectory ];
76
+ storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent: storageDir ];
77
77
return storageDirectoryPath;
78
78
}
79
79
@@ -162,10 +162,60 @@ static dispatch_queue_t RCTGetMethodQueue()
162
162
return error ? RCTMakeError (@" Failed to delete storage directory." , error, nil ) : nil ;
163
163
}
164
164
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
+ }
169
219
}
170
220
171
221
#pragma mark - RNCAsyncStorage
@@ -188,7 +238,7 @@ - (instancetype)init
188
238
if (!(self = [super init ])) {
189
239
return nil ;
190
240
}
191
- RCTPerformDirectoryMigrationCheck ();
241
+ RCTStorageDirectoryMigrationCheck ();
192
242
return self;
193
243
}
194
244
0 commit comments