0

i'm using a UICollectionView to render a list of contacts, all coming from a contacts table in the DB.

For each item, i am running a query to FMDB that returns one contact, like this:

__block CTVSimpleContact *simpleContact = [CTVSimpleContact new];

[_queue inDatabase:^(FMDatabase *db) {
    FMResultSet *result = [_db executeQuery:@"SELECT * FROM contacts "
                           @"LIMIT 1 OFFSET ?", [NSNumber numberWithInt:index]];

    while([result next]) {
        [simpleContact setContactId:[result objectForColumnName:@"id"]];
        [simpleContact setDisplayName:[result stringForColumn:@"display_name"]];
        [simpleContact setEmail:[result stringForColumn:@"email"]];
        NSString *photoUri = [result stringForColumn:@"photo_uri_low"];
        if(photoUri != nil) {
            [simpleContact setPhoto:photoUri];
        }
    }
}];

return simpleContact;

My problem is that the scrolling is extremely slow and when i profile using the Time Profiler i get that all the time is spent in the [results next] when reading the row from the table.

What would be the next step to optimize this? The scrolling is extremely laggy for around 2k Contacts.

Thanks!

albertosh
  • 2,416
  • 7
  • 25
  • 32

1 Answers1

0

I use fmdb for UICollectionView with header as NSDictionary with NSArray

@"Header1" : @[@"element1", @"element2", etc],
@"Header1" : @[@"element1", @"element2", etc],

It works very fast so, maybe try use this insted of CTVSimpleContact class.

Also, query

SELECT id, display_name, email, photo_uri_low  FROM contacts LIMIT 1 OFFSET ?

is a bit faster then

SELECT * FROM..

And, if you have something like default photo, if photo_uri_low = null you can use

IFNULL(photo_uri_low, "DEFAULT_PHOTO_URI_LOW") as photo_uri_low
Quver
  • 1,408
  • 14
  • 21