Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #20 from 3lvis/improve/drop
Browse files Browse the repository at this point in the history
Remove sqlite-shm and sqlite-wal
  • Loading branch information
3lvis committed Sep 30, 2015
2 parents 297bff2 + b7005db commit bd466d0
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions Source/DATAStack.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,39 @@ - (void)backgroundContextDidSave:(NSNotification *)backgroundContextNotification

#pragma mark - Test

- (void)drop
{
- (void)drop {
NSPersistentStore *store = [self.persistentStoreCoordinator.persistentStores lastObject];
NSURL *storeURL = store.URL;
NSString *sqliteFile = [storeURL.path stringByDeletingPathExtension];
NSFileManager *fileManager = [NSFileManager defaultManager];

self.writerContext = nil;
self.mainContext = nil;
self.persistentStoreCoordinator = nil;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *shm = [NSString stringWithFormat:@"%@.sqlite-shm", sqliteFile];
NSError *removeShmError = nil;
if ([fileManager fileExistsAtPath:shm]) {
[fileManager removeItemAtURL:[NSURL fileURLWithPath:shm] error:&removeShmError];
}
if (removeShmError) {
NSLog(@"Could not delete persitent store shm: %@", removeShmError.localizedDescription);
}

NSError *error = nil;
if ([fileManager fileExistsAtPath:store.URL.path]) [fileManager removeItemAtURL:store.URL error:&error];
NSString *wal = [NSString stringWithFormat:@"%@.sqlite-wal", sqliteFile];
NSError *removeWalError = nil;
if ([fileManager fileExistsAtPath:wal]) {
[fileManager removeItemAtURL:[NSURL fileURLWithPath:wal] error:&removeWalError];
}
if (removeWalError) {
NSLog(@"Could not delete persitent store wal: %@", removeWalError.localizedDescription);
}

if (error) {
NSError *removeStoreURLError = nil;
if ([fileManager fileExistsAtPath:storeURL.path]) {
[fileManager removeItemAtURL:storeURL error:&removeStoreURLError];
}
if (removeStoreURLError) {
NSLog(@"error deleting sqlite file");
abort();
}
Expand Down

0 comments on commit bd466d0

Please sign in to comment.