Skip to content
Browse files

Adding a bit of flexibility (in the face of failure) for core data st…

…orage implementations that can recreate data.
  • Loading branch information...
1 parent d1bc42d commit 9c813ae9812ff951b5c814ea544e6b277dafaa80 @robbiehanson committed
View
48 Extensions/XEP-0045/HybridStorage/XMPPRoomHybridStorage.m
@@ -36,7 +36,7 @@
// Log levels: off, error, warn, info, verbose
#if DEBUG
- static const int xmppLogLevel = XMPP_LOG_LEVEL_VERBOSE | XMPP_LOG_FLAG_TRACE;
+ static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#else
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif
@@ -106,13 +106,57 @@ - (void)commonInit
pausedMessageDeletion = [[NSMutableSet alloc] init];
}
+/**
+ * Documentation from the superclass (XMPPCoreDataStorage):
+ *
+ * Override me, if needed, to provide customized behavior.
+ *
+ * This method is queried to get the name of the ManagedObjectModel within the app bundle.
+ * It should return the name of the appropriate file (*.xdatamodel / *.mom / *.momd) sans file extension.
+ *
+ * The default implementation returns the name of the subclass, stripping any suffix of "CoreDataStorage".
+ * E.g., if your subclass was named "XMPPExtensionCoreDataStorage", then this method would return "XMPPExtension".
+ *
+ * Note that a file extension should NOT be included.
+**/
- (NSString *)managedObjectModelName
{
- // This method overrides [XMPPCoreDataStorage managedObjectModelName].
+ // Optional hook
+ //
+ // The default implementation would return "XMPPPRoomHybridStorage".
+ // We prefer a slightly shorter version.
return @"XMPPRoomHybrid";
}
+/**
+ * Documentation from the superclass (XMPPCoreDataStorage):
+ *
+ * Override me, if needed, to provide customized behavior.
+ *
+ * For example, if you are using the database for non-persistent data and the model changes, you may want
+ * to delete the database file if it already exists on disk and a core data migration is not worthwhile.
+ *
+ * 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.
+ *
+ * The default implementation simply writes to the XMPP error log.
+**/
+- (void)didNotAddPersistentStoreWithPath:(NSString *)storePath error:(NSError *)error
+{
+ // Optional hook
+ //
+ // If we ever have problems opening the database file,
+ // it's likely because the model changed or the file became corrupt.
+ //
+ // In this case we don't have to worry about migrating the data, because it's all stored on servers.
+ // So we're just going to delete the sqlite file from disk, and create a new one.
+
+ [[NSFileManager defaultManager] removeItemAtPath:storePath error:NULL];
+
+ [self addPersistentStoreWithPath:storePath error:NULL];
+}
+
- (void)dealloc
{
[self destroyDeleteTimer];
View
46 Extensions/XEP-0115/CoreDataStorage/XMPPCapabilitiesCoreDataStorage.m
@@ -155,26 +155,32 @@ - (void)_clearAllNonPersistentCapabilitiesForXMPPStream:(XMPPStream *)stream
#pragma mark Overrides
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-- (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;
+/**
+ * Documentation from the superclass (XMPPCoreDataStorage):
+ *
+ * Override me, if needed, to provide customized behavior.
+ *
+ * For example, if you are using the database for non-persistent data and the model changes, you may want
+ * to delete the database file if it already exists on disk and a core data migration is not worthwhile.
+ *
+ * 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.
+ *
+ * The default implementation simply writes to the XMPP error log.
+**/
+- (void)didNotAddPersistentStoreWithPath:(NSString *)storePath error:(NSError *)error
+{
+ // Optional hook
+ //
+ // If we ever have problems opening the database file,
+ // it's likely because the model changed or the file became corrupt.
+ //
+ // In this case we don't have to worry about migrating the data, because it's all stored on servers.
+ // So we're just going to delete the sqlite file from disk, and create a new one.
+
+ [[NSFileManager defaultManager] removeItemAtPath:storePath error:NULL];
+
+ [self addPersistentStoreWithPath:storePath error:NULL];
}
- (void)didCreateManagedObjectContext

0 comments on commit 9c813ae

Please sign in to comment.
Something went wrong with that request. Please try again.