0

我正在研究核心数据迁移。当持久存储不匹配并且应用程序崩溃时,我正在尝试处理异常。但在它崩溃之前,我想让用户知道(通过警报视图)他需要先卸载应用程序,然后重新安装它。我在persistentStoreCoordinator 访问器方法中有警报视图的代码,但警报视图从未出现。

这是代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{

  if (__persistentStoreCoordinator != nil) {
    return __persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ExchangeMail.sqlite"];

NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

// Dictionary required to store the details of psc on the disk,
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:&error];

// This BOOL value is required to check whether a migration is required or not.
BOOL pscCompatibile = [[self managedObjectModel] isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata];
NSLog(@"Migration needed? %d", !pscCompatibile);


NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setValue:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setValue:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Non-matching Database File" message:@"The model configuration used to open the store is compatible with the one that was used to create the store. Please Uninstall the App and try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[__persistentStoreCoordinator lock];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

 //        dispatch_queue_t queue = NULL;
 //        queue = dispatch_get_main_queue();
 //        dispatch_async(queue, ^{
 //            [[NSFileManager defaultManager]removeItemAtURL:storeURL error:nil];
 //            
 //            [self.myAlertView show];
 //        });

    [self.myAlertView show];
    //exit(-1);
}

[__persistentStoreCoordinator unlock];


if(!pscCompatibile){
    isMigrationRequired = YES;
}

return __persistentStoreCoordinator;

}

4

0 回答 0