2010-09-04 35 views
0

我不知道如何描述這個,因爲我對所有的開發人員都是新的,我真的很期待你們的回答。我知道你可能很忙,但請儘量幫助我!在啓動時加載數據庫會降低應用程序的速度

在這裏。我有一個應用程序加載一個非常大的數據庫(儘管它只有100個條目包含高分辨率圖像(100MB))。

在啓動時,tableview會顯示行 - 記錄(僅使用數據庫中的3個屬性)。但是,似乎整個數據庫(包括圖像)在啓動時加載! 有什麼方法可以在應用程序啓動時加載3個屬性(類似於「select」),然後當用戶移動到didselectrowatindexpath時加載記錄的其餘部分?

因爲我沒有想法在哪裏看或做什麼,我將不勝感激一些編碼幫助!

這裏使用的代碼IM:

#pragma mark - 
#pragma mark App support 

- (NSFetchedResultsController *)resetFetchedResultsController:(NSPredicate *)predicate cached:(BOOL)cached 
{ 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Records" inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 


    NSSortDescriptor *partDescriptor = [[NSSortDescriptor alloc] initWithKey:@"displayOrder" ascending:YES]; 
    NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: partDescriptor, nameDescriptor, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 


    if (predicate != nil) 
     [fetchRequest setPredicate:predicate]; 

    NSString *cacheName = nil; 
    if (cached) 
     cacheName = @"Root"; 

    NSFetchedResultsController *aFetchedResultsController = [[[NSFetchedResultsController alloc] 
                   initWithFetchRequest:fetchRequest 
                   managedObjectContext:managedObjectContext 
                   sectionNameKeyPath:nil 
                   cacheName:cacheName] autorelease]; 
    aFetchedResultsController.delegate = self; 

    [fetchRequest release]; 
    [partDescriptor release]; 
    [nameDescriptor release]; 
    [sortDescriptors release]; 

    NSError *error = nil; 
    if (![aFetchedResultsController performFetch:&error]) { 
     // Handle error 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 

    return aFetchedResultsController; 
} 







- (void)showRecords:(Records *)records animated:(BOOL)animated { 
    . 
    RecordsDetailViewController *detailViewController = [[RecordsDetailViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
    detailViewController.records = records; 

    [self.navigationController pushViewController:detailViewController animated:animated]; 
    [detailViewController release]; 
} 


#pragma mark - 
#pragma mark Table view methods 


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.backgroundColor = [UIColor lightGrayColor]; 
} 



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    NSInteger count = [[fetchedResultsController sections] count]; 

    if (count == 0) { 
     count = 1; 
    } 

    return count; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSInteger numberOfRows = 0; 

    if ([[fetchedResultsController sections] count] > 0) { 
     id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 
     numberOfRows = [sectionInfo numberOfObjects]; 
    } 

    return numberOfRows; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *RecordCellIdentifier = @"RecordCellIdentifier"; 

    RecordTableViewCell *recordCell = (RecordTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RecordCellIdentifier]; 
    if (recordCell == nil) { 
     recordCell = [[[RecordTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:RecordCellIdentifier] autorelease]; 
     recordCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    [self configureCell:recordCell atIndexPath:indexPath]; 

    return recordCell; 
} 


- (void)configureCell:(RecordTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    // Configure the cell 
    Records *records = (Records *)[fetchedResultsController objectAtIndexPath:indexPath]; 
    cell.records = records; 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (self.searchDisplayController.isActive) 
     [self.tableView reloadData]; 


    Records *records = (Records *)[fetchedResultsController objectAtIndexPath:indexPath]; 

    [self showRecords:records animated:YES]; 
} 

//這是從RecordTableViewCell.m顯示你的屬性即時通訊使用:再次

#pragma mark - 
#pragma mark Record set accessor 

- (void)setRecord:(Record *)newRecord { 
    if (newRecord != record) { 
     [record release]; 
     record = [newRecord retain]; 
    } 
    imageView.image = [UIImage imageNamed:@"icon.png"]; 
    nameLabel.text = record.name; 
    overviewLabel.text = record.overview; 
    partLabel.text = record.part; 
} 

感謝...

+0

SQLite本身非常樂意一次加載一個數據庫,只將內容帶入內存中是絕對必要的。問題是(幾乎可以肯定)在它上面的層中;我知道必須有這樣一個圖層,因爲你不是直接在那裏寫SQL ...... – 2010-09-05 10:02:37

回答

0

我會將大文件與元數據分開,因爲我喜歡有自由地管理這些昂貴的資源。然後我可以以不同的方式存儲它們,例如文件系統或http服務器。這允許我將它們緩存或主動將它們發送到遠程位置以減少下載時間

表的其餘部分可以適合數據庫中較少的塊,因此需要較少的磁盤訪問。許多數據庫做內部反正,如PostgreSQL的

您可以僅僅通過標識

+0

所以沒有辦法在數據庫中加載實體的某些屬性,而不是在啓動時加載整個東西? – treasure 2010-09-04 11:56:04

0

確定在這裏不言而喻指的是沉重的資源。我放棄了在啓動時單獨加載我需要的屬性的想法。 我已經做了什麼,現在無暇工作就是在我的模型中創建關係。圖像現在只在被調用時加載!

這個解決方案在我腦海中,但因爲我已經填充了我的數據庫,所以我很難重複這一步驟。

但是我很高興我做到了!

現在,它的工作原理,因爲它應該!

Happy Developer !!

相關問題