Add support, tests, and documentation for the deletion of External Storage directory when an instance of RKManagedObjectStore is reset. closes #677

This commit is contained in:
Blake Watters
2012-12-25 14:50:13 -05:00
parent 285b75ea2a
commit aecc1db352
5 changed files with 73 additions and 1 deletions

View File

@@ -140,7 +140,7 @@
error:(NSError **)error;
/**
Resets the persistent stores in the receiver's persistent store coordinator and recreates them. If a store being reset is backed by a file on disk (such as a SQLite file), the file will be removed prior to recreating the store. If the store was originally created using a seed database, the seed will be recopied to reset the store to its seeded state.
Resets the persistent stores in the receiver's persistent store coordinator and recreates them. If a store being reset is backed by a file on disk (such as a SQLite file), the file will be removed prior to recreating the store. If the store was originally created using a seed database, the seed will be recopied to reset the store to its seeded state. If the managed object model uses External Storage for any of its entities, then the external storage directory will be recursively deleted when the store is reset.
@param error On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.
@return A Boolean value indicating if the reset was successful.

View File

@@ -225,6 +225,22 @@ static RKManagedObjectStore *defaultStore = nil;
if (error) *error = localError;
return NO;
}
// Check for and remove an external storage directory
NSString *supportDirectoryName = [NSString stringWithFormat:@".%@_SUPPORT", [[URL lastPathComponent] stringByDeletingPathExtension]];
NSURL *supportDirectoryFileURL = [NSURL URLWithString:supportDirectoryName relativeToURL:[URL URLByDeletingLastPathComponent]];
BOOL isDirectory = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath:[supportDirectoryFileURL path] isDirectory:&isDirectory]) {
if (isDirectory) {
if (! [[NSFileManager defaultManager] removeItemAtURL:supportDirectoryFileURL error:&localError]) {
RKLogError(@"Failed to remove persistent store Support directory at URL %@: %@", supportDirectoryFileURL, localError);
if (error) *error = localError;
return NO;
}
} else {
RKLogWarning(@"Found external support item for store at path that is not a directory: %@", [supportDirectoryFileURL path]);
}
}
} else {
RKLogDebug(@"Skipped removal of persistent store file: URL for persistent store is not a file URL. (%@)", URL);
}

View File

@@ -271,4 +271,60 @@
}];
}
- (void)testCleanupOfExternalStorageDirectoryOnReset
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
NSPersistentStore *store = [managedObjectStore persistentStoreCoordinator].persistentStores[0];
// Check that there is a support directory
NSString *supportDirectoryName = [NSString stringWithFormat:@".%@_SUPPORT", [[store.URL lastPathComponent] stringByDeletingPathExtension]];
NSURL *supportDirectoryFileURL = [NSURL URLWithString:supportDirectoryName relativeToURL:[store.URL URLByDeletingLastPathComponent]];
BOOL isDirectory = NO;
BOOL supportDirectoryExists = [[NSFileManager defaultManager] fileExistsAtPath:[supportDirectoryFileURL path] isDirectory:&isDirectory];
expect(supportDirectoryExists).to.equal(YES);
expect(isDirectory).to.equal(YES);
NSError *error = nil;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[supportDirectoryFileURL path] error:&error];
NSDate *creationDate = attributes[NSFileCreationDate];
BOOL success = [managedObjectStore resetPersistentStores:&error];
expect(success).to.equal(YES);
attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[supportDirectoryFileURL path] error:&error];
NSDate *newCreationDate = attributes[NSFileCreationDate];
expect([creationDate laterDate:newCreationDate]).to.equal(newCreationDate);
}
- (void)testThatPersistentStoreWithLongNameHasExternalStorageResetCorrectly
{
// Create a store with an object to serve as our seed database
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] init];
NSError *error = nil;
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"This is the Store.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
assertThat(persistentStore, is(notNilValue()));
[managedObjectStore createManagedObjectContexts];
// Check that there is a support directory
NSString *supportDirectoryName = [NSString stringWithFormat:@".%@_SUPPORT", [[persistentStore.URL lastPathComponent] stringByDeletingPathExtension]];
NSString *supportDirectoryPath = [[[[persistentStore URL] path] stringByDeletingLastPathComponent] stringByAppendingPathComponent:supportDirectoryName];
BOOL isDirectory = NO;
BOOL supportDirectoryExists = [[NSFileManager defaultManager] fileExistsAtPath:supportDirectoryPath isDirectory:&isDirectory];
expect(supportDirectoryExists).to.equal(YES);
expect(isDirectory).to.equal(YES);
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:supportDirectoryPath error:&error];
NSDate *creationDate = attributes[NSFileCreationDate];
BOOL success = [managedObjectStore resetPersistentStores:&error];
expect(success).to.equal(YES);
attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:supportDirectoryPath error:&error];
NSDate *newCreationDate = attributes[NSFileCreationDate];
expect([creationDate laterDate:newCreationDate]).to.equal(newCreationDate);
}
@end