Cleanup contextWillSave method

Update deleting incompatible store
This commit is contained in:
Saul Mora
2012-10-23 23:05:52 -07:00
parent abb7314b26
commit 2eaec27410
2 changed files with 16 additions and 10 deletions

View File

@@ -162,19 +162,22 @@ static id iCloudSetupNotificationObserver = nil;
- (void) MR_obtainPermanentIDsBeforeSaving;
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contextWillSave:)
selector:@selector(MR_contextWillSave:)
name:NSManagedObjectContextWillSaveNotification
object:self];
}
- (void)contextWillSave:(NSNotification *)notification
- (void) MR_contextWillSave:(NSNotification *)notification
{
NSManagedObjectContext *context = (NSManagedObjectContext *)notification.object;
if (context.insertedObjects.count > 0) {
NSArray *insertedObjects = [[context insertedObjects] allObjects];
NSManagedObjectContext *context = [notification object];
NSSet *insertedObjects = [context insertedObjects];
if ([insertedObjects count])
{
MRLog(@"Context %@ is about to save. Obtaining permanent IDs for new %lu inserted objects", [context MR_description], (unsigned long)[insertedObjects count]);
NSError *error;
[context obtainPermanentIDsForObjects:insertedObjects error:&error];
NSError *error = nil;
[context obtainPermanentIDsForObjects:[insertedObjects allObjects] error:&error];
[MagicalRecord handleErrors:error];
}
}

View File

@@ -77,10 +77,12 @@ NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagica
if (!store && [MagicalRecord shouldDeleteStoreOnModelMismatch])
{
if ([error.domain isEqualToString:NSCocoaErrorDomain] &&
[error code] == NSMigrationMissingSourceModelError) {
if ([[error domain] isEqualToString:NSCocoaErrorDomain] && [error code] == NSPersistentStoreIncompatibleVersionHashError)
{
// Could not open the database, so... kill it!
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
MRLog(@"Removed incompatible model version: %@", [url lastPathComponent]);
// Try one more time to create the store
store = [self addPersistentStoreWithType:NSSQLiteStoreType
@@ -88,7 +90,8 @@ NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagica
URL:url
options:options
error:&error];
if (store) {
if (store)
{
// If we successfully added a store, remove the error that was initially created
error = nil;
}