2012-04-26 42 views
0

我一直在追捕的最后四小时后的解决方案,我在绝望中张贴。我的应用程序在模拟器,我的iPhone和我的iPad上工作完美,直到我为我的某个数据模型添加了一个属性。崩溃时保存在设备上更改数据模型

我的iPhone应用程序使用核心数据和iCloud中。在AppDelegate中,我通过合并两个模型来创建managedObjectModel。一切似乎都很好,直到我试图保存ManagedObjectContext。这是当它与崩溃:

*终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,理由是:“这 NSPersistentStoreCoordinator没有持久性存储。它不能 执行保存操作。'

这不会发生在模拟器上。

我曾尝试:

  • 项目 - >干净生成文件夹
  • 项目 - >清洁
  • 从我 设备
  • 删除从我的iCloud icloud的数据备份
  • 删除app
  • 重新启动计算机
  • 改“.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; 

}

+0

由于时间限制,我选择重置我的设备。该应用程序按预期工作。我仍然想找到一个解决方案,因为这是一个很容易发生的问题。 – Blamdarot 2012-04-26 20:27:06

回答

0

通常情况下,它应该删除的程序数据之后还好。但是,错误表明,您没有不一致性,但根本没有商店。假设:如果发生故障,您不会重新启动它。也许,下面的代码可能很有用:

- (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; 
} 
+0

感谢您的回复。不幸的是,我不确定如何将它集成到我的代码中。我的persistentStoreCoordinator getter方法有很多iCloud代码。我会将我的代码添加到我的原始帖子中。 – Blamdarot 2012-04-26 07:31:28

相关问题