diff options
Diffstat (limited to 'Classes/TopicsViewController.m')
-rw-r--r-- | Classes/TopicsViewController.m | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/Classes/TopicsViewController.m b/Classes/TopicsViewController.m index 9751eb2..d9c2f60 100644 --- a/Classes/TopicsViewController.m +++ b/Classes/TopicsViewController.m @@ -19,6 +19,8 @@ #import "SectionDictionary.h" #import "FMDatabase.h" #import "FMResultSet.h" +#import "PicCastAppDelegate.h" + @implementation TopicsViewController @@ -40,7 +42,7 @@ // Create a file cache for the object manager to use // A real app might do this durring startup, allowing the object manager and cache to be shared by several screens - NSString* cacheDirectory = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/imgcache/flickr/"] ; + NSString* cacheDirectory = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/imgcache/icons/"] ; HJMOFileCache* fileCache = [[[HJMOFileCache alloc] initWithRootPath:cacheDirectory] autorelease]; objMan.fileCache = fileCache; @@ -51,18 +53,28 @@ tableDictionary = [[[SectionDictionary alloc] init] retain]; - NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleDB" ofType:@"sqlite"]; - FMDatabase *db = [[FMDatabase alloc] initWithPath:path]; - [db open]; - [self getTopicsFromDb]; [self startParsing]; } +- (void) openDb { + db = [[FMDatabase databaseWithPath:[PicCastAppDelegate getDatabasePath]] retain]; + [db setShouldCacheStatements:YES]; + [db setTraceExecution:true]; + [db setLogsErrors:true]; + [db open]; +} + +- (void) closeDb { + [db close]; + db = nil; +} + - (void) getTopicsFromDb { // grab topics - FMResultSet *fResult= [db executeQuery:@"SELECT * FROM picCasts"]; //order by date, limite _max + [self openDb]; + FMResultSet *fResult = [db executeQuery:@"SELECT * FROM picCasts"]; //order by date, limite _max // put into sections while([fResult next]) @@ -70,13 +82,12 @@ [self addTopicToSectionDictionary:[Topic initFromDatabaseRow:fResult]]; } - [fResult close]; - + [fResult close]; + [self closeDb]; // put into data structure } - (void) addTopicToSectionDictionary:(Topic *)topic { - NSLog(@"days apart: %d", [self daysApart:topic.releaseDate]); switch ((int)[self daysApart:topic.releaseDate]) { case 0: [tableDictionary appendObject:topic forKey:@"Today"]; @@ -113,21 +124,26 @@ // called after parsing is finished - (void) updateTopicsFromWeb:(NSArray *)parsedTopics { // add topics to sql - [db beginTransaction]; + [self openDb]; for (Topic *topic in parsedTopics) { // if topic is already in database - FMResultSet *rs = [db executeQuery:@"SELECT id FROM picCasts WHERE foreignId = ?", 10]; - if (![rs hasAnotherRow]) { + //FMResultSet *rs = [db executeQuery:@"SELECT id FROM picCasts WHERE foreignId = ?", [NSNumber numberWithInt:10]]; + if (true) { + //[rs close]; + [db beginTransaction]; + NSLog(@"tamp"); [topic serializeToDatabase:db]; + [db commit]; + NSLog(@"%@", [db lastErrorMessage]); + NSLog(@"%d", [db lastInsertRowId]); // add topics to structure [self addTopicToSectionDictionary:topic]; } } - [db commit]; -// NSLog(@"%@", [db lastErrorMessage]); -// NSLog(@"%d", [db lastInsertRowId]); + [self closeDb]; + // tell table to animate things being added - [self.tableView reloadData]; + [self.tableView reloadData]; } - (void) sourcesEditViewControllerDidFinish:(SourcesEditViewController *)controller { @@ -199,7 +215,7 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. - NSLog(@"number of sections: %d", [tableDictionary count]); + //NSLog(@"number of sections: %d", [tableDictionary count]); return [tableDictionary count]; } @@ -220,12 +236,12 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. - NSLog(@"number of rows in section %d: %d", section, [[tableDictionary objectForIndex:section] count]); + //NSLog(@"number of rows in section %d: %d", section, [[tableDictionary objectForIndex:section] count]); return [[tableDictionary objectForIndex:section] count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { - NSLog(@"row title: %@", [tableDictionary keyForIndex:section]); + //NSLog(@"row title: %@", [tableDictionary keyForIndex:section]); return [tableDictionary keyForIndex:section]; //how do you convert a key to index from mutabledictionary } @@ -373,6 +389,7 @@ } - (void)dealloc { + [db release]; [tableDictionary release]; [objMan release]; [topics release]; |