1

コアデータベースを使用しています。IOS6で動作していますが、IOS5でテストしようとすると何も起こりません。私がしていることを説明させてください。

まず、viewWillAppearでこれを行います。

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"view appeared");
    [super viewWillAppear:animated];
    if (!self.genkDatabase) {
        NSLog(@"comes to here");
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        url = [url URLByAppendingPathComponent:@"Default appGenk Database2"];
        // url is now "<Documents Directory>/Default Photo Database"
        self.genkDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; // setter will create this for us on disk
        NSLog(@"database created on disk");
    }

}

次に、UseDocumentメソッドに入ります。

- (void)useDocument
{
    NSLog(@"Comses in the use document");
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.genkDatabase.fileURL path]]) {
        // does not exist on disk, so create it
        [self.genkDatabase saveToURL:self.genkDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            NSLog(@"create");
            [self setupFetchedResultsController];
           [self fetchGenkDataIntoDocument:self.genkDatabase];

        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
        NSLog(@"closed news");
        // exists on disk, but we need to open it
        [self.genkDatabase openWithCompletionHandler:^(BOOL success) {
            [self setupFetchedResultsController];
        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
        NSLog(@"normal");
        // already open and ready to use
        [self setupFetchedResultsController];
    }

}

そして最後に、それはsetDatabaseメソッドに入ります。

- (void)setGenkDatabase:(UIManagedDocument *)genkDatabase
{
    if (_genkDatabase != genkDatabase) {
        _genkDatabase = genkDatabase;
        [self useDocument];
    }
    NSLog(@"Comes in the setdatabase methode.");
}

これをすべて行うと、次のログが得られます。

2012-10-22 10:42:47.444 RacingGenk[4786:c07] view appeared
2012-10-22 10:42:47.445 RacingGenk[4786:c07] comes to here
2012-10-22 10:42:47.459 RacingGenk[4786:c07] Comses in the use document
2012-10-22 10:42:47.460 RacingGenk[4786:c07] Comes in the setdatabase methode.
2012-10-22 10:42:47.461 RacingGenk[4786:c07] database created on disk

ご覧のとおり、使用ドキュメントの作成ログは印刷されません。したがって、メソッドFetchDataIntoDocumentを実行することはできません。

誰かがこの問題で私を助けることができますか?私は今のところこの問題を何年もの間探しています。

前進してくれてありがとう。

ステフ

4

1 に答える 1

1

シミュレーターまたはデバイスでテストしていますか?シミュレータでは大文字と小文字が区別されません。デバイスではファイルアクセスで大文字と小文字が区別されます。覚えておいてください。

ただし、NSFileManagerのドキュメントでは、ファイルが存在するかどうかを確認するのではなく、ファイルを読み取ってエラー(たとえば、ファイルが見つからないエラー)を適切に処理することを推奨しています。したがって、ファイルが存在するかどうかを確認するのではなく、ファイルをロードしてみてください。

また、データベースファイルのファイル拡張子が表示されません。「デフォルトのappGenkDatabase2」とだけ表示されます。これはすでにファイル名ですか、それともこれまでのところ別のサブディレクトリですか?

編集:

次のコードを試すことができます。

- (void) setupStore {
    NSString* storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"mydatabase.sqlite"];

    // Set up the store.
    NSFileManager* fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, create one.
    if (![fileManager fileExistsAtPath: storePath]) {
        // create your store here!
    }
}


- (NSString*) applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

「mydatabase.sqlite」は、ドキュメントディレクトリに存在すると予想されるデータベースファイルの名前です。

コアデータ永続ストアをセットアップする方法の本格的な例を見たい場合は、アップル独自のiPhoneCoreDataRecipesの例を確認できます。見てください

RecipesAppDelegate.m

実装。

于 2012-10-22T09:55:10.973 に答える