2010-09-04 78 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 !!

相关问题