1

在这个应用程序中,我使用的是由另一个应用程序创建的 sqlite 数据库。我可以使用 Firefox SQLite 管理器查询数据库,并查看我正在搜索的内容确实存在于数据库中。我已将查询简化为非常简单的内容,但在我的 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)

在 fetch 之后立即检查aFetchedResultsController(分配给 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 回答