mirror of
https://github.com/zhigang1992/MagicalRecord.git
synced 2026-01-12 09:13:42 +08:00
Cleanup contextWillSave method
Update deleting incompatible store
This commit is contained in:
@@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user