Wednesday, 21 August 2013

Improve the performance of uitableview creation with many sections from sqlite database

Improve the performance of uitableview creation with many sections from
sqlite database

This is my scenario:
Sqlite database with 30000 products with 800 different brands.
Table product is like this:
id | title | brand | other fields
Products example:
1221 | Product 1 | brand 1 | ...
1222 | Product 2 | brand 2 | ...
1223 | Product 3 | brand 2 | ...
1224 | Product 4 | brand 3 | ...
1225 | Product 5 | brand 3 | ...
1226 | Product 6 | brand 3 | ...
I need to create a uiviewtable with all products in different sections.
Sections are different brands of products.
I must implements this methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
My idea to solve this problem is as follows (written in pseudocode):
NSArray * productList = [databaseManager getProductList]; //array with
Product model objects
NSArray * brandsList = [databaseManager getBrandsList];
NSMutableDictionary * dictionaryProductBrands = [NSMutableDictionary
dictionaryWithCapacity:bramdsList.count];
for (NSString *brand in brandsList) {
NSMutableArray *arrayProductsPerBrands = [NSMutableArray array];
for (Product *p in productList) {
if ([p.brand isEqualToString:brand]) {
[arrayProductsPerBrands addObject:p];
}
}
[dictionaryProductBrands setValue:arrayProductsPerBrands forKey:brand];
}
This solution has a poor performance.
Can I improve the way to generate the tableview with sections with the
same database?

No comments:

Post a Comment