Skip to content

Instantly share code, notes, and snippets.

@betamike
Created August 17, 2011 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betamike/1152515 to your computer and use it in GitHub Desktop.
Save betamike/1152515 to your computer and use it in GitHub Desktop.
fmdb test case
//test method which roughly mimics how I normally fetch results
-(NSTimeInterval)fetchResultsTest
{
static NSString * const selectFavorites = @"SELECT songId, title FROM songs LIMIT 50";
NSMutableArray *fetched = [NSMutableArray array];
FMResultSet *resultSet = [[self database] executeQuery:selectFavorites];
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
while ([resultSet next])
{
Song *song = [Song songFromResultSet:resultSet];
[fetched addObject:song];
}
NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
return (end - start);
}
-(void)runTest
{
NSTimeInterval processTotal = 0;
for (int i = 0; i < 100; i++)
{
processTotal += [self fetchResultsTest];
}
NSLog(@"Average time: %f", (processTotal/100));
}
+(Song *)songFromResultSet:(FMResultSet *)resultSet
{
return [[[Song alloc] initWithResultSet:resultSet] autorelease];
}
-(id)initWithResultSet:(FMResultSet *)resultSet
{
if ((self = [self init]))
{
[self setSongID:[resultSet objectForColumnName:@"songId"]];
[self setTitle:[resultSet stringForColumn:@"title"]];
//a couple of columns that may not be present from all result sets
if ([resultSet columnIndexForName:@"offlineStatus"] != -1)
{
offlineStatus = [resultSet intForColumn:@"offlineStatus"];
}
if ([resultSet columnIndexForName:@"isInLibrary"] != -1)
{
inLibrary = [resultSet boolForColumn:@"isInLibrary"];
}
}
return self;
}
CREATE TABLE songs (songId INTEGER PRIMARY KEY UNIQUE NOT NULL, title TEXT NOT NULL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment