From 0c7a7586d3c2ab22adb15f032b0a69c962aaeeaf Mon Sep 17 00:00:00 2001 From: matt handler Date: Mon, 25 Apr 2011 00:27:48 -0400 Subject: we all good son, well, at least its writing to the db --- Classes/FMDatabase.m | 2 +- Classes/Topic.m | 6 +++-- Classes/TopicsViewController.h | 2 ++ Classes/TopicsViewController.m | 55 +++++++++++++++++++++++++++--------------- 4 files changed, 43 insertions(+), 22 deletions(-) (limited to 'Classes') diff --git a/Classes/FMDatabase.m b/Classes/FMDatabase.m index 46d963b..77a99a4 100644 --- a/Classes/FMDatabase.m +++ b/Classes/FMDatabase.m @@ -4,7 +4,7 @@ @implementation FMDatabase + (id)databaseWithPath:(NSString*)aPath { - return [[[self alloc] initWithPath:aPath] autorelease]; + return [[self alloc] initWithPath:aPath]; } - (id)initWithPath:(NSString*)aPath { diff --git a/Classes/Topic.m b/Classes/Topic.m index 65ea506..123df3f 100644 --- a/Classes/Topic.m +++ b/Classes/Topic.m @@ -52,18 +52,20 @@ } - (void)serializeToDatabase:(FMDatabase *)db { - //NSLog(@"title: %@", title); + [db retain]; + NSLog(@"title: %@", title); [db executeUpdate:@"INSERT INTO picCasts \ (foreignId, link, title, description, releaseDate, category, iconUrl, picCount) \ VALUES \ (?, ?, ?, ?, ?, ?, ?, ?)", - 12, guid, title, description, [Topic dateToString:releaseDate], category, picCount]; + [NSNumber numberWithInt:12], guid, title, description, [Topic dateToString:releaseDate], category, iconUrl, [NSNumber numberWithInt:12]]; //NSLog(@"insert id:%d error: %@", [db lastInsertRowId], [db lastErrorMessage]); // [NSString stringWithFormat:@"number %d", i], // [NSNumber numberWithInt:i], // [NSDate date], // [NSNumber numberWithFloat:2.2f]]; + [db release]; } - (void)dealloc { diff --git a/Classes/TopicsViewController.h b/Classes/TopicsViewController.h index c6de22f..78b154f 100644 --- a/Classes/TopicsViewController.h +++ b/Classes/TopicsViewController.h @@ -36,5 +36,7 @@ // Called by the ParserChoiceViewController based on the selected parser type. - (void)startParsing; +- (void)getTopicsFromDb; +- (void)addTopicToSectionDictionary:(Topic *)topic; @end 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]; -- cgit v1.2.3