Skip to content

Commit

Permalink
XMPPCoreDataStorage initWithDatabaseFilename: storeOptions: API
Browse files Browse the repository at this point in the history
  • Loading branch information
ObjColumnist committed Sep 10, 2013
1 parent 4c7be49 commit adb7515
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 60 deletions.
13 changes: 10 additions & 3 deletions Extensions/CoreDataStorage/XMPPCoreDataStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
@protected

NSString *databaseFileName;
NSDictionary *storeOptions;
NSUInteger saveThreshold;
NSUInteger saveCount;

Expand All @@ -68,11 +69,11 @@
* If you pass nil, a default database filename is automatically used.
* This default is derived from the classname,
* meaning subclasses will get a default database filename derived from the subclass classname.
*
*
* If you attempt to create an instance of this class with the same databaseFileName as another existing instance,
* this method will return nil.
**/
- (id)initWithDatabaseFilename:(NSString *)databaseFileName;
**/
- (id)initWithDatabaseFilename:(NSString *)databaseFileName storeOptions:(NSDictionary *)storeOptions;

/**
* Initializes a core data storage instance, backed by an in-memory store.
Expand All @@ -85,6 +86,12 @@
**/
@property (readonly) NSString *databaseFileName;

/**
* Readonly access to the databaseOptions used during initialization.
* If nil was passed to the init method, returns the actual databaseOptions being used (the default databaseOptions).
**/
@property (readonly) NSDictionary *storeOptions;

/**
* The saveThreshold specifies the maximum number of unsaved changes to NSManagedObjects before a save is triggered.
*
Expand Down
58 changes: 38 additions & 20 deletions Extensions/CoreDataStorage/XMPPCoreDataStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,32 @@ - (NSString *)defaultDatabaseFileName
{
// Override me, if needed, to provide customized behavior.
//
// This method is queried if the initWithDatabaseFileName method is invoked with a nil parameter.
// This method is queried if the initWithDatabaseFileName:storeOptions: method is invoked with a nil parameter for databaseFileName.
//
// You are encouraged to use the sqlite file extension.

return [NSString stringWithFormat:@"%@.sqlite", [self managedObjectModelName]];
}

- (void)willCreatePersistentStoreWithPath:(NSString *)storePath
- (NSDictionary *)defaultStoreOptions
{

// Override me, if needed, to provide customized behavior.
//
// This method is queried if the initWithDatabaseFileName:storeOptions: method is invoked with a nil parameter for defaultStoreOptions.

NSDictionary *defaultStoreOptions = nil;

if(databaseFileName)
{
defaultStoreOptions = @{ NSMigratePersistentStoresAutomaticallyOption: @(YES),
NSInferMappingModelAutomaticallyOption : @(YES) };
}

return defaultStoreOptions;
}

- (void)willCreatePersistentStoreWithPath:(NSString *)storePath options:(NSDictionary *)theStoreOptions
{
// Override me, if needed, to provide customized behavior.
//
Expand All @@ -113,7 +131,7 @@ - (void)willCreatePersistentStoreWithPath:(NSString *)storePath
// If this instance was created via initWithInMemoryStore, then the storePath parameter will be nil.
}

- (BOOL)addPersistentStoreWithPath:(NSString *)storePath error:(NSError **)errorPtr
- (BOOL)addPersistentStoreWithPath:(NSString *)storePath options:(NSDictionary *)theStoreOptions error:(NSError **)errorPtr
{
// Override me, if needed, to completely customize the persistent store.
//
Expand All @@ -131,16 +149,10 @@ - (BOOL)addPersistentStoreWithPath:(NSString *)storePath error:(NSError **)error

NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

// Default support for automatic lightweight migrations
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];

persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
options:storeOptions
error:errorPtr];
}
else
Expand All @@ -157,7 +169,7 @@ - (BOOL)addPersistentStoreWithPath:(NSString *)storePath error:(NSError **)error
return persistentStore != nil;
}

- (void)didNotAddPersistentStoreWithPath:(NSString *)storePath error:(NSError *)error
- (void)didNotAddPersistentStoreWithPath:(NSString *)storePath options:(NSDictionary *)theStoreOptions error:(NSError *)error
{
// Override me, if needed, to provide customized behavior.
//
Expand Down Expand Up @@ -225,6 +237,7 @@ - (void)mainThreadManagedObjectContextDidMergeChanges
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@synthesize databaseFileName;
@synthesize storeOptions;

- (void)commonInit
{
Expand All @@ -245,17 +258,22 @@ - (void)commonInit

- (id)init
{
return [self initWithDatabaseFilename:nil];
return [self initWithDatabaseFilename:nil storeOptions:nil];
}

- (id)initWithDatabaseFilename:(NSString *)aDatabaseFileName
- (id)initWithDatabaseFilename:(NSString *)aDatabaseFileName storeOptions:(NSDictionary *)theStoreOptions
{
if ((self = [super init]))
{
if (aDatabaseFileName)
databaseFileName = [aDatabaseFileName copy];
else
databaseFileName = [[self defaultDatabaseFileName] copy];

if(theStoreOptions)
storeOptions = theStoreOptions;
else
storeOptions = [self defaultStoreOptions];

if (![[self class] registerDatabaseFileName:databaseFileName])
{
Expand Down Expand Up @@ -559,22 +577,22 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
// If storePath is nil, then NSURL will throw an exception

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

NSError *error = nil;

BOOL didAddPersistentStore = [self addPersistentStoreWithPath:storePath error:&error];
BOOL didAddPersistentStore = [self addPersistentStoreWithPath:storePath options:storeOptions error:&error];

if(autoRecreateDatabaseFile && !didAddPersistentStore)
{
[[NSFileManager defaultManager] removeItemAtPath:storePath error:NULL];

didAddPersistentStore = [self addPersistentStoreWithPath:storePath error:&error];
didAddPersistentStore = [self addPersistentStoreWithPath:storePath options:storeOptions error:&error];
}

if (!didAddPersistentStore)
{
[self didNotAddPersistentStoreWithPath:storePath error:error];
[self didNotAddPersistentStoreWithPath:storePath options:storeOptions error:error];
}
}
else
Expand All @@ -587,12 +605,12 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
// In-Memory persistent store

[self willCreatePersistentStoreWithPath:nil];
[self willCreatePersistentStoreWithPath:nil options:storeOptions];

NSError *error = nil;
if (![self addPersistentStoreWithPath:nil error:&error])
if (![self addPersistentStoreWithPath:nil options:storeOptions error:&error])
{
[self didNotAddPersistentStoreWithPath:nil error:error];
[self didNotAddPersistentStoreWithPath:nil options:storeOptions error:error];
}
}

Expand Down
20 changes: 16 additions & 4 deletions Extensions/CoreDataStorage/XMPPCoreDataStorageProtected.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
/**
* Override me, if needed, to provide customized behavior.
*
* This method is queried if the initWithDatabaseFileName method is invoked with a nil parameter.
* This method is queried if the initWithDatabaseFileName:storeOptions: method is invoked with a nil parameter for databaseFileName.
* The default implementation returns:
*
* [NSString stringWithFormat:@"%@.sqlite", [self managedObjectModelName]];
Expand All @@ -52,6 +52,18 @@
**/
- (NSString *)defaultDatabaseFileName;


/**
* Override me, if needed, to provide customized behavior.
*
* This method is queried if the initWithDatabaseFileName:storeOptions method is invoked with a nil parameter for storeOptions.
* The default implementation returns the following:
*
* @{ NSMigratePersistentStoresAutomaticallyOption: @(YES),
* NSInferMappingModelAutomaticallyOption : @(YES) };
**/
- (NSDictionary *)defaultStoreOptions;

/**
* Override me, if needed, to provide customized behavior.
*
Expand All @@ -63,7 +75,7 @@
*
* The default implementation does nothing.
**/
- (void)willCreatePersistentStoreWithPath:(NSString *)storePath;
- (void)willCreatePersistentStoreWithPath:(NSString *)storePath options:(NSDictionary *)storeOptions;

/**
* Override me, if needed, to completely customize the persistent store.
Expand All @@ -74,7 +86,7 @@
* If this instance was created via initWithDatabaseFilename, then the storePath parameter will be non-nil.
* If this instance was created via initWithInMemoryStore, then the storePath parameter will be nil.
**/
- (BOOL)addPersistentStoreWithPath:(NSString *)storePath error:(NSError **)errorPtr;
- (BOOL)addPersistentStoreWithPath:(NSString *)storePath options:(NSDictionary *)storeOptions error:(NSError **)errorPtr;

/**
* Override me, if needed, to provide customized behavior.
Expand All @@ -87,7 +99,7 @@
*
* The default implementation simply writes to the XMPP error log.
**/
- (void)didNotAddPersistentStoreWithPath:(NSString *)storePath error:(NSError *)error;
- (void)didNotAddPersistentStoreWithPath:(NSString *)storePath options:(NSDictionary *)storeOptions error:(NSError *)error;

/**
* Override me, if needed, to provide customized behavior.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
/* Inherited from XMPPCoreDataStorage
* Please see the XMPPCoreDataStorage header file for extensive documentation.
- (id)initWithDatabaseFilename:(NSString *)databaseFileName;
- (id)initWithDatabaseFilename:(NSString *)databaseFileName storeOptions:(NSDictionary *)storeOptions;
- (id)initWithInMemoryStore;
@property (readonly) NSString *databaseFileName;
Expand Down
7 changes: 4 additions & 3 deletions Extensions/Roster/CoreDataStorage/XMPPRosterCoreDataStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ + (XMPPRosterCoreDataStorage *)sharedInstance
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

sharedInstance = [[XMPPRosterCoreDataStorage alloc] initWithDatabaseFilename:nil];
sharedInstance = [[XMPPRosterCoreDataStorage alloc] initWithDatabaseFilename:nil storeOptions:nil];
});

return sharedInstance;
Expand All @@ -48,7 +48,8 @@ - (void)commonInit
[super commonInit];

// This method is invoked by all public init methods of the superclass

autoRecreateDatabaseFile = YES;

rosterPopulationSet = [[NSMutableSet alloc] init];
}

Expand Down Expand Up @@ -132,7 +133,7 @@ - (void)_clearAllResourcesForXMPPStream:(XMPPStream *)stream
#pragma mark Overrides
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (void)willCreatePersistentStore:(NSString *)filePath
- (void)willCreatePersistentStoreWithPath:(NSString *)filePath options:(NSDictionary *)theStoreOptions
{
// This method is overriden from the XMPPCoreDataStore superclass.
// From the documentation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/* Inherited from XMPPCoreDataStorage
* Please see the XMPPCoreDataStorage header file for extensive documentation.
- (id)initWithDatabaseFilename:(NSString *)databaseFileName;
- (id)initWithDatabaseFilename:(NSString *)databaseFileName storeOptions:(NSDictionary *)storeOptions;
- (id)initWithInMemoryStore;
@property (readonly) NSString *databaseFileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ + (XMPPRoomCoreDataStorage *)sharedInstance
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

sharedInstance = [[XMPPRoomCoreDataStorage alloc] initWithDatabaseFilename:nil];
sharedInstance = [[XMPPRoomCoreDataStorage alloc] initWithDatabaseFilename:nil storeOptions:nil];
});

return sharedInstance;
Expand Down
2 changes: 1 addition & 1 deletion Extensions/XEP-0045/HybridStorage/XMPPRoomHybridStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ + (XMPPRoomHybridStorage *)sharedInstance
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

sharedInstance = [[XMPPRoomHybridStorage alloc] initWithDatabaseFilename:nil];
sharedInstance = [[XMPPRoomHybridStorage alloc] initWithDatabaseFilename:nil storeOptions:nil];
});

return sharedInstance;
Expand Down
24 changes: 2 additions & 22 deletions Extensions/XEP-0054/CoreDataStorage/XMPPvCardCoreDataStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ + (XMPPvCardCoreDataStorage *)sharedInstance
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

sharedInstance = [[XMPPvCardCoreDataStorage alloc] initWithDatabaseFilename:nil];
sharedInstance = [[XMPPvCardCoreDataStorage alloc] initWithDatabaseFilename:nil storeOptions:nil];
});

return sharedInstance;
Expand All @@ -64,29 +64,9 @@ - (BOOL)configureWithParent:(XMPPvCardTempModule *)aParent queue:(dispatch_queue
- (void)commonInit
{
autoAllowExternalBinaryDataStorage = YES;
autoRecreateDatabaseFile = YES;
[super commonInit];
}
- (BOOL)addPersistentStoreWithPath:(NSString *)storePath error:(NSError **)errorPtr
{
BOOL result = [super addPersistentStoreWithPath:storePath error:errorPtr];

if (!result &&
[*errorPtr code] == NSMigrationMissingSourceModelError &&
[[*errorPtr domain] isEqualToString:NSCocoaErrorDomain]) {
// If we get this error while trying to add the persistent store, it most likely means the model changed.
// Since we are caching capabilities, it is safe to delete the persistent store and create a new one.

if ([[NSFileManager defaultManager] fileExistsAtPath:storePath])
{
[[NSFileManager defaultManager] removeItemAtPath:storePath error:nil];

// Try creating the store again, without creating a deletion/creation loop.
result = [super addPersistentStoreWithPath:storePath error:errorPtr];
}
}

return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark XMPPvCardAvatarStorage protocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/* Inherited from XMPPCoreDataStorage
* Please see the XMPPCoreDataStorage header file for extensive documentation.
- (id)initWithDatabaseFilename:(NSString *)databaseFileName;
- (id)initWithDatabaseFilename:(NSString *)databaseFileName storeOptions:(NSDictionary *)storeOptions;
- (id)initWithInMemoryStore;
@property (readonly) NSString *databaseFileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ + (XMPPCapabilitiesCoreDataStorage *)sharedInstance
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

sharedInstance = [[XMPPCapabilitiesCoreDataStorage alloc] initWithDatabaseFilename:nil];
sharedInstance = [[XMPPCapabilitiesCoreDataStorage alloc] initWithDatabaseFilename:nil storeOptions:nil];
});

return sharedInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
/* Inherited from XMPPCoreDataStorage
* Please see the XMPPCoreDataStorage header file for extensive documentation.
- (id)initWithDatabaseFilename:(NSString *)databaseFileName;
- (id)initWithDatabaseFilename:(NSString *)databaseFileName storeOptions:(NSDictionary *)storeOptions;
- (id)initWithInMemoryStore;
@property (readonly) NSString *databaseFileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ + (XMPPMessageArchivingCoreDataStorage *)sharedInstance
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

sharedInstance = [[XMPPMessageArchivingCoreDataStorage alloc] initWithDatabaseFilename:nil];
sharedInstance = [[XMPPMessageArchivingCoreDataStorage alloc] initWithDatabaseFilename:nil storeOptions:nil];
});

return sharedInstance;
Expand Down

0 comments on commit adb7515

Please sign in to comment.