【问题标题】:Crash When Saving On Device After Changing Datamodel更改数据模型后在设备上保存时崩溃
【发布时间】:2012-05-06 20:24:01
【问题描述】:

在过去的四个小时里,我一直在寻找解决方案,我绝望地发帖。我的应用程序在模拟器、我的 iPhone 和我的 ipad 上运行良好,直到我为我的一个数据模型添加了一个属性。

我的 iPhone 应用程序使用 Core Data 和 iCloud。在 AppDelegate 中,我通过合并两个模型来创建 managedObjectModel。在我尝试保存 ManagedObjectContext 之前,一切似乎都很好。那是它崩溃的时候:

* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“这个 NSPersistentStoreCoordinator 没有持久存储。这不可以 执行保存操作。'

这不会在模拟器上发生。

我试过了:

  • 项目->清理构建文件夹
  • 项目->清理
  • 从我的应用程序中删除 设备
  • 从我的 iCloud 备份中删除 iCloud 数据
  • 重启电脑
  • 将“.momd”更改为“.mom”并再次返回(在另一个问题中阅读)

感谢您的帮助。

编辑添加代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (__persistentStoreCoordinator != nil) {
    return __persistentStoreCoordinator;
}

__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];    
NSPersistentStoreCoordinator *psc = __persistentStoreCoordinator;

// TODO: Set up iCloud in another thread:
//dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *dataFileName = [NSString stringWithFormat:@"%@.sqlite", APP_TITLE_NO_SPACES];

NSString *iCloudDataDirectoryName = @"Data.nosync";
NSString *iCloudLogsDirectoryName = @"Logs";
NSFileManager *fileManager = [NSFileManager defaultManager];        
NSURL *localStore = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:dataFileName];
NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];

if (iCloud) {
    NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];
    if([fileManager fileExistsAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]] == NO) {
        NSError *fileSystemError;
        [fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName] 
               withIntermediateDirectories:YES 
                                attributes:nil 
                                     error:&fileSystemError];
        if(fileSystemError != nil) {
            NSLog(@"Error creating database directory %@", fileSystemError);
        }
    }

    NSString *iCloudData = [[[iCloud path] 
                             stringByAppendingPathComponent:iCloudDataDirectoryName] 
                            stringByAppendingPathComponent:dataFileName];

    NSLog(@"iCloudData = %@", iCloudData);

    NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                             CLOUD_CONTAINER_IDENTIFIER, NSPersistentStoreUbiquitousContentNameKey,
                             iCloudLogsPath, NSPersistentStoreUbiquitousContentURLKey, nil];

    [psc lock];
    [psc addPersistentStoreWithType:NSSQLiteStoreType 
                      configuration:nil 
                                URL:[NSURL fileURLWithPath:iCloudData] 
                            options:options 
                              error:nil];
    [psc unlock];
} else {
    NSLog(@"iCloud is NOT working - using a local store");
    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];

    [psc lock];

    [psc addPersistentStoreWithType:NSSQLiteStoreType 
                      configuration:nil 
                                URL:localStore 
                            options:options 
                              error:nil];
    [psc unlock];
}
__persistentStoreCoordinator = psc;

[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ICLOUD_SOMETHING_CHANGED object:nil];

return __persistentStoreCoordinator;

}

【问题讨论】:

  • 由于时间限制,我选择重置我的设备。该应用程序按预期工作。我仍然想找到一个解决方案,因为这是一个将来很容易发生的问题。

标签: iphone ios core-data crash datamodel


【解决方案1】:

通常,删除程序数据后就可以了。但是,错误表明,您没有不一致,但根本没有存储。假设:如果出现故障,您不会重新启动它。也许,下面的代码会很有用:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

{
    if (__persistentStoreCoordinator != nil) {
        return __persistentStoreCoordinator;
    }
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myApp.sqlite"];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        // Error, erase data
        [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
        NSLog(@"store cleaned");
        __persistentStoreCoordinator = nil;
        return [self persistentStoreCoordinator];
    }    
    return __persistentStoreCoordinator;
} 

【讨论】:

  • 感谢您的回复。不幸的是,我不确定如何将它集成到我的代码中。我的 persistentStoreCoordinator getter 方法有很多 iCloud 代码。我会将我的代码添加到我的原始帖子中。
猜你喜欢
  • 2015-03-03
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多