2012-04-26 63 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

相關問題