2012-04-26 8 views
0

私は過去4時間の解決策を探していましたが、私は絶望的に投稿しています。私のアプリケーションは、データモデルの1つに属性を追加するまで、シミュレータ、iPhone、およびipadで完全に動作していました。データモデルの変更後にデバイスに保存するとクラッシュする

私のiPhoneアプリケーションは、Core DataとiCloudを使用しています。 AppDelegateでは、2つのモデルをマージしてmanagedObjectModelを作成します。 ManagedObjectContextを保存しようとするまで、すべてうまく見えます。 「この NSPersistentStoreCoordinatorは、永続的な店舗を持っていない:キャッチされない例外により「NSInternalInconsistencyException」、理由にアプリを終了

*:それはとクラッシュするときです。保存操作を実行することはできません。

これはシミュレータでは発生しません。

私が試してみました:

  • プロジェクト - >クリーンビルドフォルダ
  • プロジェクト - >バックアップ
  • 私のiCloudからのiCloudデータを削除して、私の デバイス
  • からアプリを削除
  • クリーン
  • コンピュータを再起動
  • ".momd"を ".mom"に変更してもう一度元に戻す(別の質問で読む)

ありがとうございました。

EDITコードを追加するには:

- (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

答えて

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コードがあります。自分のコードを私のorignal投稿に追加します。 – Blamdarot

関連する問題