Sqlite 启动时加载数据库会减慢应用程序的速度

Sqlite 启动时加载数据库会减慢应用程序的速度,sqlite,select,tableviewcell,Sqlite,Select,Tableviewcell,我不知道该如何描述这一点,因为我对所有的发展都是新手,我真的很期待你们的回答。我知道你可能很忙,但请尽量帮助我 来了。我有一个应用程序,加载了一个非常大的数据库(虽然它只有100个条目,但它包含了租用图像(100MB)) 启动时,tableview显示行-记录(仅使用数据库中的3个属性)。但是,整个数据库(包括图像)似乎是在启动时加载的! 有没有办法在应用程序启动时只加载3个属性(如“选择”),然后在用户移动到didselectrowatindexpath时加载记录的其余部分 因为我不知道去哪里

我不知道该如何描述这一点,因为我对所有的发展都是新手,我真的很期待你们的回答。我知道你可能很忙,但请尽量帮助我

来了。我有一个应用程序,加载了一个非常大的数据库(虽然它只有100个条目,但它包含了租用图像(100MB))

启动时,tableview显示行-记录(仅使用数据库中的3个属性)。但是,整个数据库(包括图像)似乎是在启动时加载的! 有没有办法在应用程序启动时只加载3个属性(如“选择”),然后在用户移动到didselectrowatindexpath时加载记录的其余部分

因为我不知道去哪里看或做什么,我将感谢一些编码帮助

以下是我使用的代码:

#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];
}
#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;
}

再次感谢…

我会将大文件与元数据分开,因为我喜欢有自由分别管理这些昂贵的资源。然后我可以以不同的方式存储它们,例如文件系统或http服务器。这使我能够缓存它们或主动将它们发送到远程位置,以减少下载时间

然后,表的其余部分可以放入数据库中更少的块中,因此需要更少的磁盘访问。许多数据库在内部都这样做,例如postgresql


您只需通过Id引用繁重的资源即可

好了,开始了。我已经放弃了在启动时分别加载所需属性的想法。 我所做的和现在完美的工作就是在我的模型中创建关系。图像现在仅在调用时加载

我想到了这个解决方案,但是因为我已经填充了我的数据库,所以我很难重复这个步骤

然而,我很高兴我做到了

现在它可以正常工作了


开发者快乐

因此,没有办法只在数据库中加载实体的某些属性,而不是在启动时加载整个内容?SQLite本身非常乐意一次加载一个数据库,只在严格必要时将内容放入内存。问题(几乎可以肯定)在它上面的那层;我知道一定有这样一个层,因为您不是直接在那里编写SQL…