只读 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]);
}