I have a unique case where I need to populate a menu with thousands of items from an sqlite database...
I am currently filling it with the following code, but it blocks the main thread while its populating, and takes upwards of a minute to load (the first time) as it fills with ~3000 items. Is there an effective way to lazy load the menu in smaller chunks without blocking the ui?
rules.h
{
IBOutlet NSPopUpButton *rulesSelectionButton;
}
@property (retain) IBOutlet NSPopUpButton *rulesSelectionButton;
rules.m
@synthesize rulesSelectionButton;
and the code to populate the button:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *selected = [defaults objectForKey:rulesSelectionString];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *path = [dbPath stringByExpandingTildeInPath];
__block NSArray *final = [NSArray new];
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:path];
[queue inDatabase:^(FMDatabase *db) {
FMResultSet *results = [db executeQuery:@"SELECT * FROM rules"];
NSMutableArray *array = [NSMutableArray new];
[array addObject:NSLocalizedString(@"Random...", nil)];
while ([results next]) {
@autoreleasepool {
NSString *title = [NSString stringWithFormat:@"%@ (%@)", [results stringForColumn:@"Organization_Rules"], [results stringForColumn:@"Assignment"]];
[array addObject:title];
}
}
final = [[NSSet setWithArray:array] allObjects];
}];
NSArray *sortedKeys = [final sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
dispatch_async(dispatch_get_main_queue(), ^{
[rulesSelectionButton removeAllItems];
[rulesSelectionButton addItemsWithTitles:sortedKeys];
[rulesSelectionButton selectItemWithTitle:selected];
});
});