2012-02-26 12 views
-1

誰かがこれを見つけたのかどうか疑問に思うだけです。coredataはxcode 4.3で動作していませんか?

私は以前のxcodeバージョンでは素晴らしく働いていたこのコードを手に入れました。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

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

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"mydb.sqlite"]; 
    /* 
    Set up the store. 
    For the sake of illustration, provide a pre-populated default store. 
    */ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    // If the expected store doesn't exist, copy the default store. 
    if (![fileManager fileExistsAtPath:storePath]) { 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
     } 
    } 

    NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 

    [self addSkipBackupAttributeToItemAtURL:storeUrl]; 

    NSError *error; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

     Typical reasons for an error here include: 
     * The persistent store is not accessible 
     * The schema for the persistent store is incompatible with current managed object model 
     Check the error message to determine what the actual problem was. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return persistentStoreCoordinator; 
} 

私はこのことから次のことを期待する:

  1. 私のバンドルには「mydb.sqlite」がない場合はゼロから作成される空の「mydbという」。
  2. "mydb.sqlite"がメインバンドルに存在する場合は、指定されたディレクトリにコピーされると考えられます。
  3. "mydb.sqlite"が自分のxcdatamodelと互換性がない場合、アプリはクラッシュする必要があります。

ただしこれは既に作成済みのdbでのみ動作します。 たとえば、 "mydb.sqlite"という名前のランダムなDBをバンドルに入れて元のファイルを削除しようとすると、

アプリがクラッシュしません!!!空のdbが作成され、新しいdbは無視されます。 これは私のコードに反するので、これは完全に間違っています。

さらに、元のデータベースを追加しても何も起きず、アプリケーションは空のdbを作成します。

(はい、私は、私のプロジェクトをきれいシムアプリを削除し、さらに任意の変更が発生する前にビルドフォルダを削除します!)

任意のアイデア?

+0

あなたのエラーは何ですか?それを掲示できますか? – Raptor

+0

エラーはありません。それは私のコードによるべきである方法を機能しないのちょうどの観察 –

答えて

0

質問/見解への回答は簡単です。私は4.2.1に私のXcodeを復元(タイムマシンのために神に感謝)となりましALLいる4.2.1

格下げは予想通りです。

今日、Appleにバグレポートをアップさせていただきます。

0

あなたのコードと期待通りに違いがあります。

はここにあなたの期待です:

  1. 私のバンドルには「mydb.sqlite」がない場合はゼロから作成される空の「mydbという」。
  2. "mydb.sqlite"がメインバンドルに存在する場合、指定されたディレクトリにコピーされると考えられます。
  3. "mydb.sqlite"が自分のxcdatamodelと互換性がない場合、アプリはクラッシュする必要があります。ここで

はあなたのコードが何をするかである:それは内に存在する場合Documentsディレクトリ

  • mydb.sqliteが存在しない場合でmydb.sqliteため

    1. ルックは、(メインバンドルからそれをコピーしますメインバンドル)
    2. は〜/書類/ mydb.sqliteで

    私はあなたが経験していると思う主な問題は、ステップ3. 01の新しい永続ストアを作成します。は、提供されたURLに新しい店舗を作成します。代わりに、メインバンドルからコピーされたファイルが存在することを確認するためにファイルの存在を再クエリする必要があります。存在する場合は、新しい永続ストアを作成する代わりに[persistentStoreCoordinator persistentStoreForURL:storeURL]を使用してください。

    これを修正したら、他の問題をより簡単に追跡できるようにする必要があります。コードにNSLogを追加することをお勧めします。そうすれば、コードが期待する実行パスに従っているかどうかを確認できます。例えば。作成した新しいオブジェクトをそれぞれログに記録して、いずれかがnilかどうかを簡単に確認します。バンドルdbをコピーするときは、NULLではなくNSErrorポインタを追加し、エラーがnilでなければログに記録します。

  • +0

    はい申し訳ありません。あなたが何を期待しているかを述べるもの。私は私の質問でそれを明確にしませんでした。しかし、これは問題ありません。 –

    0

    デフォルトのdbが実際にバンドルに含まれていることを確認します。つまり、プロジェクトに含まれるとしてチェックされ、コピーファイルのビルドフェーズにあります。私はプロジェクトとSQLiteファイルを再配置し、プロジェクト構造にコピーしたものは私のプロジェクトには含まれていませんでした。これはあなたが見ている動作を引き起こします。

    関連する問題