0

これを開発中のすべての新しいものとして説明する方法がわかりません。皆さんからの回答を本当に楽しみにしています。お忙しいとは存じますが、どうか私を助けてください!

ここに行きます。非常に大きなデータベースをロードするアプリがあります (100 エントリしかありませんが、HiRes 画像 (100MB) が含まれています)。

起動時に、テーブルビューは行レコードを表示します (データベースから 3 つの属性のみを使用)。ただし、起動時にデータベース全体 (画像を含む) が読み込まれるようです! アプリの起動時に 3 つの属性 (「select」など) のみをロードし、ユーザーが 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];
}

// これは 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;
}

再度、感謝します...

4

2 に答える 2

0

これらの高価なリソースを個別に管理する自由が欲しいので、大きなファイルをメタデータから分離します。次に、ファイルシステムや http サーバーなど、別の方法で保存できます。これにより、それらをキャッシュするか、事前にリモートの場所に送信して、ダウンロード時間を短縮できます

テーブルの残りの部分は、データベース内のより少ないブロックに収まるため、必要なディスク アクセスが少なくなります。とにかく、多くのデータベースはこれを内部的に行います。たとえば、postgresql

Idで重いリソースを参照するだけです

于 2010-09-04T11:21:41.890 に答える
0

Ok here it goes. I ve gave up the idea of loading separately the attributes that i need at start up. What i ve done AND NOW WORKS FLAWLESSLY is to do create RELATIONSHIPS in my model. Images are now loading only when called!

This solution was in my mind but because i had already populated my database it was difficult for me to repeat this step.

However i m glad that i did!

NOW it works as it should!!

Happy Developer!!

于 2010-09-05T10:19:10.680 に答える