Skip to content

Commit 68386d6

Browse files
committed
XMPPCoreDataStorage autoRemovePreviousDatabaseFile API
1 parent 88be4cc commit 68386d6

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Extensions/CoreDataStorage/XMPPCoreDataStorage.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
NSUInteger saveThreshold;
5757
NSUInteger saveCount;
5858

59-
BOOL autoRecreateDatabaseFile;
59+
BOOL autoRemovePreviousDatabaseFile;
60+
BOOL autoRecreateDatabaseFile;
6061
BOOL autoAllowExternalBinaryDataStorage;
6162

6263
dispatch_queue_t storageQueue;
@@ -136,6 +137,14 @@
136137
**/
137138
@property (strong, readonly) NSManagedObjectContext *mainThreadManagedObjectContext;
138139

140+
/**
141+
* The Previous Database File is removed before creating a persistant store.
142+
*
143+
* Default NO
144+
**/
145+
146+
@property (readwrite) BOOL autoRemovePreviousDatabaseFile;
147+
139148
/**
140149
* The Database File is automatically recreated if the persistant store cannot read it e.g. the model changed or the file became corrupt.
141150
* For greater control overide didNotAddPersistentStoreWithPath:

Extensions/CoreDataStorage/XMPPCoreDataStorage.m

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,14 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
576576
if (storePath)
577577
{
578578
// If storePath is nil, then NSURL will throw an exception
579+
580+
if(autoRemovePreviousDatabaseFile)
581+
{
582+
if ([[NSFileManager defaultManager] fileExistsAtPath:storePath])
583+
{
584+
[[NSFileManager defaultManager] removeItemAtPath:storePath error:nil];
585+
}
586+
}
579587

580588
[self willCreatePersistentStoreWithPath:storePath options:storeOptions];
581589

@@ -743,6 +751,34 @@ - (void)managedObjectContextDidSave:(NSNotification *)notification
743751
}
744752
}
745753

754+
- (BOOL)autoRemovePreviousDatabaseFile
755+
{
756+
__block BOOL result = NO;
757+
758+
dispatch_block_t block = ^{ @autoreleasepool {
759+
result = autoRemovePreviousDatabaseFile;
760+
}};
761+
762+
if (dispatch_get_specific(storageQueueTag))
763+
block();
764+
else
765+
dispatch_sync(storageQueue, block);
766+
767+
return result;
768+
}
769+
770+
- (void)setAutoRemovePreviousDatabaseFile:(BOOL)flag
771+
{
772+
dispatch_block_t block = ^{
773+
autoRemovePreviousDatabaseFile = flag;
774+
};
775+
776+
if (dispatch_get_specific(storageQueueTag))
777+
block();
778+
else
779+
dispatch_sync(storageQueue, block);
780+
}
781+
746782
- (BOOL)autoRecreateDatabaseFile
747783
{
748784
__block BOOL result = NO;
@@ -842,7 +878,6 @@ - (void)maybeSave:(int32_t)currentPendingRequests
842878
{
843879
NSAssert(dispatch_get_specific(storageQueueTag), @"Invoked on incorrect queue");
844880

845-
846881
if ([[self managedObjectContext] hasChanges])
847882
{
848883
if (currentPendingRequests == 0)

0 commit comments

Comments
 (0)