@@ -99,12 +99,17 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray<NSDictionary *> *
99
99
return storageDirectory;
100
100
}
101
101
102
+ static NSString *RCTCreateManifestFilePath (NSString *storageDirectory)
103
+ {
104
+ return [RCTCreateStorageDirectoryPath (storageDirectory) stringByAppendingString: RCTManifestFileName];
105
+ }
106
+
102
107
static NSString *RCTGetManifestFilePath ()
103
108
{
104
109
static NSString *manifestFilePath = nil ;
105
110
static dispatch_once_t onceToken;
106
111
dispatch_once (&onceToken, ^{
107
- manifestFilePath = [ RCTGetStorageDirectory () stringByAppendingPathComponent: RCTManifestFileName] ;
112
+ manifestFilePath = RCTCreateManifestFilePath (RCTStorageDirectory) ;
108
113
});
109
114
return manifestFilePath;
110
115
}
@@ -174,44 +179,70 @@ static dispatch_queue_t RCTGetMethodQueue()
174
179
return error ? RCTMakeError (@" Failed to delete storage directory." , error, nil ) : nil ;
175
180
}
176
181
182
+ static NSDate *RCTManifestModificationDate (NSString *manifestFilePath)
183
+ {
184
+ NSDictionary *attributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: manifestFilePath error: nil ];
185
+ return [attributes fileModificationDate ];
186
+ }
177
187
178
188
NSString *const RCTOldStorageDirectory = @" RNCAsyncLocalStorage_V1" ;
179
189
/* *
180
190
* Creates an NSException used during Storage Directory Migration.
181
191
*/
182
- static void RCTStorageDirectoryMigrationLogError (NSString *reason, NSError *error) {
192
+ static void RCTStorageDirectoryMigrationLogError (NSString *reason, NSError *error)
193
+ {
183
194
NSLog (@" %@ : %@ " , reason, error ? error.description : @" " );
184
195
}
185
196
197
+ static void RCTStorageDirectoryCleanupOld ()
198
+ {
199
+ NSError *error;
200
+ if (![[NSFileManager defaultManager ] removeItemAtPath: RCTCreateStorageDirectoryPath (RCTOldStorageDirectory) error: &error]) {
201
+ RCTStorageDirectoryMigrationLogError (@" Failed to remove old storage directory during migration" , error);
202
+ }
203
+ }
204
+
205
+ static void RCTStorageDirectoryMigrate ()
206
+ {
207
+ NSError *error;
208
+ // Migrate data by copying old storage directory to new storage directory location
209
+ if (![[NSFileManager defaultManager ] copyItemAtPath: RCTCreateStorageDirectoryPath (RCTOldStorageDirectory) toPath: RCTGetStorageDirectory () error: &error]) {
210
+ RCTStorageDirectoryMigrationLogError (@" Failed to copy old storage directory to new storage directory location during migration" , error);
211
+ } else {
212
+ // If copying succeeds, remove old storage directory
213
+ RCTStorageDirectoryCleanupOld ();
214
+ }
215
+ }
216
+
186
217
/* *
187
218
* This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data.
188
219
* Data is migrated from the "RNCAsyncLocalStorage_V1" directory to the "RCTAsyncLocalStorage_V1" directory.
189
220
*/
190
- static void RCTStorageDirectoryMigrationCheck () {
221
+ static void RCTStorageDirectoryMigrationCheck ()
222
+ {
191
223
static dispatch_once_t onceToken;
192
224
dispatch_once (&onceToken, ^{
193
225
NSError *error;
194
226
BOOL isDir;
195
- // If the old directory exists, it means we need to migrate data to the new directory
227
+ // If the old directory exists, it means we may need to migrate old data to the new directory
196
228
if ([[NSFileManager defaultManager ] fileExistsAtPath: RCTCreateStorageDirectoryPath (RCTOldStorageDirectory) isDirectory: &isDir] && isDir) {
197
229
// Check if the new storage directory location already exists
198
- BOOL newStorageDirectoryExists = [[NSFileManager defaultManager ] fileExistsAtPath: RCTGetStorageDirectory () isDirectory: &isDir];
199
- if (newStorageDirectoryExists) {
200
- // If the new storage directory location already exists, remove existing directory
201
- newStorageDirectoryExists = !([[NSFileManager defaultManager ] removeItemAtPath: RCTGetStorageDirectory () error: &error]);
202
- if (newStorageDirectoryExists) {
203
- RCTStorageDirectoryMigrationLogError (@" Failed to clear pre-existing storage directory" , error);
204
- }
205
- }
206
- if (!newStorageDirectoryExists) {
207
- // If new storage direction doesn't exist, copy old storage directory to new location
208
- if (![[NSFileManager defaultManager ] copyItemAtPath: RCTCreateStorageDirectoryPath (RCTOldStorageDirectory) toPath: RCTGetStorageDirectory () error: &error]) {
209
- RCTStorageDirectoryMigrationLogError (@" Failed to copy old storage directory to new storage directory" , error);
230
+ if ([[NSFileManager defaultManager ] fileExistsAtPath: RCTGetStorageDirectory ()]) {
231
+ // If new storage location exists, check if the new storage has been modified sooner
232
+ if ([RCTManifestModificationDate (RCTGetManifestFilePath ()) compare: RCTManifestModificationDate (RCTCreateManifestFilePath (RCTOldStorageDirectory))] == 1 ) {
233
+ // If new location has been modified more recently, simply clean out old data
234
+ RCTStorageDirectoryCleanupOld ();
210
235
} else {
211
- // If copying succeeds, remove old storage directory
212
- [[NSFileManager defaultManager ] removeItemAtPath: RCTCreateStorageDirectoryPath (RCTOldStorageDirectory) error: &error];
213
- if (error) RCTStorageDirectoryMigrationLogError (@" Failed to remove old storage directory after migration" , error);
236
+ // If old location has been modified more recently, remove new storage and migrate
237
+ if (![[NSFileManager defaultManager ] removeItemAtPath: RCTGetStorageDirectory () error: &error]) {
238
+ RCTStorageDirectoryMigrationLogError (@" Failed to remove new storage directory during migration" , error);
239
+ } else {
240
+ RCTStorageDirectoryMigrate ();
241
+ }
214
242
}
243
+ } else {
244
+ // If new storage location doesn't exist, migrate data
245
+ RCTStorageDirectoryMigrate ();
215
246
}
216
247
}
217
248
});
@@ -228,7 +259,8 @@ @implementation RNCAsyncStorage
228
259
NSMutableDictionary <NSString *, NSString *> *_manifest;
229
260
}
230
261
231
- + (BOOL )requiresMainQueueSetup {
262
+ + (BOOL )requiresMainQueueSetup
263
+ {
232
264
return NO ;
233
265
}
234
266
0 commit comments