Added helper methods for deleting managed object stores

This commit is contained in:
Blake Watters
2012-03-28 12:50:56 -04:00
parent a545c3942b
commit 8aedbeb1a5
2 changed files with 38 additions and 0 deletions

View File

@@ -74,6 +74,25 @@ extern NSString* const RKManagedObjectStoreDidFailSaveNotification;
+ (RKManagedObjectStore *)defaultObjectStore;
+ (void)setDefaultObjectStore:(RKManagedObjectStore *)objectStore;
///-----------------------------------------------------------------------------
/// @name Deleting Store Files
///-----------------------------------------------------------------------------
/**
Deletes the SQLite file backing an RKManagedObjectStore instance at a given path.
@param path The complete path to the store file to delete.
*/
+ (void)deleteStoreAtPath:(NSString *)path;
/**
Deletes the SQLite file backing an RKManagedObjectStore instance with a given
filename within the application data directory.
@param filename The name of the file within the application data directory backing a managed object store.
*/
+ (void)deleteStoreInApplicationDataDirectoryWithFilename:(NSString *)filename;
///-----------------------------------------------------------------------------
/// @name Initializing an Object Store
///-----------------------------------------------------------------------------

View File

@@ -72,6 +72,25 @@ static RKManagedObjectStore *defaultObjectStore = nil;
[NSManagedObjectContext setDefaultContext:objectStore.primaryManagedObjectContext];
}
+ (void)deleteStoreAtPath:(NSString *)path
{
NSURL* storeURL = [NSURL fileURLWithPath:path];
NSError* error = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:storeURL.path]) {
if (! [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error]) {
NSAssert(NO, @"Managed object store failed to delete persistent store : %@", error);
}
} else {
RKLogWarning(@"Asked to delete persistent store but no store file exists at path: %@", storeURL.path);
}
}
+ (void)deleteStoreInApplicationDataDirectoryWithFilename:(NSString *)filename
{
NSString *path = [[RKDirectory applicationDataDirectory] stringByAppendingPathComponent:filename];
[self deleteStoreAtPath:path];
}
+ (RKManagedObjectStore*)objectStoreWithStoreFilename:(NSString*)storeFilename {
return [self objectStoreWithStoreFilename:storeFilename usingSeedDatabaseName:nil managedObjectModel:nil delegate:nil];
}