使用只读sqlite数据库进行慢速Core-Data获取操作

时间:2013-08-05 10:20:31

标签: ios sqlite core-data nsfetchedresultscontroller nsfetchrequest

使用只读sqlite数据库进行核心数据提取操作很慢。我可以改善其表现吗?

背景:我有一个包含2个配置的核心数据模型。一个用于默认应用程序存储,另一个用于种子数据(SeedData.sqlite)。 SeedData.sqlite放在应用程序包中,并设置为只读存储。 SeedData.sqlite包含大约6500条记录。

这是我设置持久存储的方式:

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    NSLog(@"%s", __FUNCTION__);

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

    // Attempt to load the persistent store
    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    // Create the default/ user model persistent store
    {
        NSString *storeFileName = [JTCoreDataManager defaultStoreName];
        NSString *configuration = @"AppName";
        NSURL *storeURL = [[self applicationLocalDatabaseDirectory] URLByAppendingPathComponent:storeFileName];

        // Define the Core Data version migration options
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                                 nil];

        if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                       configuration:configuration
                                                                 URL:storeURL
                                                             options:options
                                                               error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }

    // Create the seed data persistent store
    {
        NSURL *seedDataURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SeedData" ofType:@"sqlite"]];
        NSString *configuration = @"SeedData";

        // Define the Core Data version migration options
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSReadOnlyPersistentStoreOption,
                                 nil];

        if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                       configuration:configuration
                                                                 URL:seedDataURL
                                                             options:options
                                                               error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort();
            abort();
        }
    }

    return _persistentStoreCoordinator;
}

获取的结果控制器设置如下:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Seed"];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"magicOn == %@", [NSNumber numberWithBool:YES]];

[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                                           managedObjectContext:self.managedObjectContext 
                                                                                             sectionNameKeyPath:nil
                                                                                                      cacheName:nil];

NSError *error;
BOOL success = [fetchedResultsController performFetch:&error];

if (!success) {
    NSLog(@"%@", [error localizedDescription]);
}

0 个答案:

没有答案