は、私はライン上NZombieを取得しています理由として失われたビット午前:メモリ管理アドバイス - ゾンビを扱う方法?以下のサンプルコードで
[Category getInitialDataToDisplay:[self getDBPath]];
私はSOの記事やその他の文書を通じて見えたが、Objective-Cのと午前に新しいですしていますこれに私の車輪を回転させる。
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//Copy database to the user's phone if needed.
[self copyDatabaseIfNeeded];
// Init the Array
activeCategories = [[NSMutableArray alloc] init];
activeSubjects = [[NSMutableArray alloc] init];
categories = [[NSMutableArray alloc] init];
subjects = [[NSMutableArray alloc] init];
quotes = [[NSMutableArray alloc] init];
quoteMaps = [[NSMutableArray alloc] init];
//Initialize the Category array.
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.categories = tempArray;
[tempArray release];
//Once the db is copied, get the initial data to display on the screen.
[Category getInitialDataToDisplay:[self getDBPath]];
//populate active subjects and categories:
activeCategories = [self getActiveCategories];
activeSubjects = [self getActiveSubjects];
// sort data
NSSortDescriptor *categorySorter;
NSSortDescriptor *subjectSorter;
categorySorter = [[NSSortDescriptor alloc]initWithKey:@"category_title" ascending:YES];
subjectSorter = [[NSSortDescriptor alloc]initWithKey:@"title" ascending:YES];
NSArray *sortDescriptorsCat = [NSArray arrayWithObject:categorySorter];
NSArray *sortDescriptorsSub = [NSArray arrayWithObject:subjectSorter];
[self.categories sortUsingDescriptors:sortDescriptorsCat];
[self.subjects sortUsingDescriptors:sortDescriptorsSub];
[categorySorter release];
[subjectSorter release];
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
...ここ
- (void)dealloc {
[activeSubjects release];
[activeCategories release];
[categories autorelease];
[subjects autorelease];
[quotes autorelease];
[quoteMaps autorelease];
[navigationController release];
[window release];
[super dealloc];
}
はgetInitialDataToDisplayです:ここでは
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
// Use this section to bring in database and populate the array
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
//appDelegate.categories = [appDelegate.categories sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
//POPULATE THE SUBJECT
FMResultSet *result_subjects = [database executeQuery:@"select * from SUBJECT"];
while([result_subjects next]) {
NSInteger primaryKey = [result_subjects intForColumn:@"SUBJECT_ID"];
Subject *sub = [[Subject alloc] initWithPrimaryKey:primaryKey];
sub.title = [result_subjects stringForColumn:@"SUBJECT"];
sub.category_title = [result_subjects stringForColumn:@"CATEGORY"];
sub.active = [result_subjects intForColumn:@"ACTIVE"];
sub.isDirty = NO;
[appDelegate.subjects addObject:sub];
[sub release];
}
FMResultSet *result_categories = [database executeQuery:@"select distinct category from SUBJECT"];
while([result_categories next]) {
Category *cat = [[Category alloc] init];
cat.category_title = [result_categories stringForColumn:@"CATEGORY"];
NSLog(@"loading category: %@", cat.category_title);
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
for (Subject *sb in appDelegate.subjects){
if([cat.category_title isEqualToString:sb.category_title]){
[cat.subjects addObject:sb];
NSLog(@" Adding subject: %@ cat.subjects.count=%i", sb.title, cat.subjects.count);
}
}
[appDelegate.categories addObject:cat];
[cat release];
}
//POPULATE THE QUOTES
FMResultSet *result_quotes = [database executeQuery:@"select * from QUOTE"];
while([result_quotes next]) {
Quote *sub = [Quote alloc];
sub.quote_id = [result_quotes stringForColumn:@"QUOTE_ID"];
sub.quote_date = [result_quotes stringForColumn:@"DATE"];
sub.title = [result_quotes stringForColumn:@"DESC1"];
sub.desc2 = [result_quotes stringForColumn:@"DESC2"];
sub.excerpt = [result_quotes stringForColumn:@"EXCERPT"];
sub.note = [result_quotes stringForColumn:@"NOTES"];
sub.isDirty = NO;
[appDelegate.quotes addObject:sub];
[sub release];
}
//POPULATE THE QUOTE_MAPS
FMResultSet *result_quote_map = [database executeQuery:@"select * from QUOTE_MAP"];
while([result_quote_map next]) {
QuoteMap *sub = [QuoteMap alloc];
sub.quote_id = [result_quote_map stringForColumn:@"QUOTE_ID"];
sub.quote_map_id = [result_quote_map stringForColumn:@"QUOTE_MAP_ID"];
sub.subject_id = [result_quote_map stringForColumn:@"SUBJECT_ID"];
sub.isDirty = NO;
[appDelegate.quoteMaps addObject:sub];
[sub release];
}
[database close];
NSLog(@"Count of categories: %i", appDelegate.categories.count);
NSLog(@"Count of subjects: %i", appDelegate.subjects.count);
NSLog(@"Count of quotes: %i", appDelegate.quotes.count);
NSLog(@"Count of quoteMaps: %i", appDelegate.quoteMaps.count);
}
はgetDbPathです:
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"reference.db"];
}
....それが役立つと思いますか?また、カテゴリgetInitialDataToDisplay:classメソッドを表示することもできます。 – Jamie
とあなたの[self getDBPath]メソッド。 – Jamie
また、deallocで[object ** autorelease **]を使用しないでください。 [オブジェクトの解放]を使用します。 –