1

このアプリでは、別のアプリで作成された sqlite データベースを使用しています。Firefox SQLite Manager を使用してデータベースにクエリを実行すると、探しているものがデータベースに存在することがわかります。クエリを非常に単純なものに減らしましたが、NSFetchedResultsController には何も返されません。

これが私のコードです:

- (NSFetchedResultsController*) frc {

    if (!frc_) {
        @autoreleasepool {

            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
            NSEntityDescription * entity = [NSEntityDescription entityForName:@"INDEX" inManagedObjectContext:[ManagedObjectContext moc]];
            [fetchRequest setEntity:entity];
            [fetchRequest setFetchBatchSize:15];

            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lemma = 'dog'"];

            [NSFetchedResultsController deleteCacheWithName:nil];
            [fetchRequest setPredicate:predicate];

            NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"lemma" ascending:YES];
            NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
            [fetchRequest setSortDescriptors:sortDescriptors];

            NSFetchedResultsController *aFetchedResultsController =
            [[NSFetchedResultsController alloc]
             initWithFetchRequest:fetchRequest
             managedObjectContext:[ManagedObjectContext moc]
             sectionNameKeyPath:@"lemma"
             cacheName:nil];

            aFetchedResultsController.delegate = (id<NSFetchedResultsControllerDelegate>)self;

            NSError *error = nil;

            if (![aFetchedResultsController performFetch:&error]) {
                NSLog(@"Unresolved Error %@, %@", error, [error userInfo]);
                abort();
            }
            self.frc = aFetchedResultsController;
        }
    }

    return frc_;

}

データモデルには「INDEX」というエンティティがあります。エンティティにプロパティがあることを確認するために、lemmaそのプロパティの内容を表示して、次のlemmaように取得します。

po [[entity propertiesByName] objectForKey:@"lemma"]

(id) $3 = 0x1104f740 (<NSAttributeDescription: 0x1104f740>), name lemma, isOptional 1, isTransient 0, entity INDEX, renamingIdentifier lemma, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)

aFetchedResultsControllerfetch の直後 (self.frc に割り当てられている場所)の内容を調べると、次のようになります。

po aFetchedResultsController
(NSFetchedResultsController *) $4 = 0x1105c5c0 <NSFetchedResultsController: 0x1105c5c0>

po [[aFetchedResultsController fetchedObjects] count]
(id) $1 = 0x00000000 <nil>

ここでの問題は非常に基本的なものだと思いますが、何を見落としているのかわかりません。

4

1 に答える 1

1

Ray Wenderlichのサイトで答えを見つけました。問題は、sqlite ファイルをアプリケーション バンドルからアプリケーション ドキュメント ディレクトリに移動する必要があることです。

これは、バンドルからドキュメント ディレクトリにコピーすることによって行われます (ドキュメント ディレクトリがまだそこにない場合)。

永続ストア コーディネーターを作成するコードは次のとおりです。

- (NSPersistentStoreCoordinator *)pstore {
    if (pstore_ != nil) {
        return pstore_;
    }
    NSString *storePath     = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"words.sqlite"];
    NSURL *storeUrl = [NSURL fileURLWithPath: storePath];

    // THIS IS THE KEY PIECE TO IMPORTING AN EXISTING SQLITE FILE:
    // Put down default db if it doesn't already exist
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:storePath]) {
        NSString *defaultStorePath = [[NSBundle mainBundle]
                                      pathForResource:@"words" ofType:@"sqlite"];
        if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
        }
    }


    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    NSError *error = nil;
    pstore_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.mom];
    if(![pstore_ addPersistentStoreWithType:NSSQLiteStoreType
                              configuration:nil URL:storeUrl options:options error:&error]) {
        // Error for store creation should be handled in here
    }

    return pstore_;
}
于 2012-11-19T01:06:14.970 に答える