0

我在这里遵循了本教程https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections并且能够创建一个带有部分的漂亮表格,但是加载需要很长时间!好吧,不是永远,确切地说是 5 分钟。我的解析表中有 587 行,将所有对象加载到部分中需要 5 分钟。前几分钟在空白视图上显示“正在加载...”,然后是一个空的 tableview,最后所有对象都加载了。像这样的事情需要这么长时间是有原因的吗?我不能让我的用户等待 5 分钟才能加载某些内容。此表格视图在注册过程中显示。这是一个学校列表,新用户必须选择他们来自哪所学校。各科根据位置组织学校,大约有30个科。有什么建议可以让这个加载更快吗?

这是 SchoolFinderViewController.m 文件的代码

#import "SchoolFinderViewController.h"

@interface SchoolFinderViewController ()

@property (nonatomic, retain) NSMutableDictionary *sections;
@property (nonatomic, retain) NSMutableDictionary *sectionToRegionMap;

@end


@implementation SchoolFinderViewController

@synthesize sections = _sections;
@synthesize sectionToRegionMap = _sectionToRegionMap;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {

        self.parseClassName = @"School";
        self.textKey = @"Name";
        self.pullToRefreshEnabled = NO;
        self.paginationEnabled = YES;
        self.objectsPerPage = 600;
        self.sections = [NSMutableDictionary dictionary];
        self.sectionToRegionMap = [NSMutableDictionary dictionary];
    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Schools";
}


#pragma mark - PFQueryTableViewController

- (void)objectsDidLoad:(NSError *)error {
    [super objectsDidLoad:error];

    // This method is called every time objects are loaded from Parse via the PFQuery
    NSLog(@"Count in objectsDidLoad: %lu", (unsigned long)[self.objects count]);
    [self.sections removeAllObjects];
    [self.sectionToRegionMap removeAllObjects];

    NSInteger section = 0;
    NSInteger rowIndex = 0;
    int i = 0;
    for (PFObject *object in self.objects) {
        PFObject *obj = [object objectForKey:@"region"];
        [obj fetchIfNeeded];
        NSLog(@"School %@", [object objectForKey:@"Name"]);
        NSString *Region = [obj objectForKey:@"name"];
        NSLog(@"Reg: %@", Region);
        NSMutableArray *objectsInSection = [self.sections objectForKey:Region];
        if (!objectsInSection) {
            objectsInSection = [NSMutableArray array];

            NSLog(@"Is this called? %d", i);
            // this is the first time we see this Region - increment the section index
            [self.sectionToRegionMap setObject:Region forKey:[NSNumber numberWithInt:section++]];
        }

        [objectsInSection addObject:[NSNumber numberWithInt:rowIndex++]];
        [self.sections setObject:objectsInSection forKey:Region];
    }

    NSLog(@"Finally done...");
}

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    query.cachePolicy = kPFCachePolicyCacheThenNetwork;

    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    if (self.objects.count == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    // Order by name
    [query orderByAscending:@"Name"];
    return query;
}

- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
    NSString *Region = [self RegionForSection:indexPath.section];
    NSArray *rowIndecesInSection = [self.sections objectForKey:Region];
    NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row];
    return [self.objects objectAtIndex:[rowIndex intValue]];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *Region = [self RegionForSection:section];
    NSArray *rowIndecesInSection = [self.sections objectForKey:Region];
    return rowIndecesInSection.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *Region = [self RegionForSection:section];
    return Region;
}


#pragma mark - UITableViewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSLog(@"CellFor %ld", (long)indexPath.row);


    cell.textLabel.text = [object objectForKey:@"Name"];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];

    PFObject *selectedObject = [self objectAtIndexPath:indexPath];
}


#pragma mark - ()

- (NSString *)RegionForSection:(NSInteger)section {
    return [self.sectionToRegionMap objectForKey:[NSNumber numberWithInt:section]];
}
4

1 回答 1

1

是的,您将无法按原样进行足够快的操作...客户端不必先下载每个对象,并且包含 500 多个项目的滚动列表并不是一个好的用户体验。也许您应该有一个初始屏幕,他们可以在其中选择一些子集,然后他们可以在下一个屏幕上查询较小的数据集。您当前用作部分的内容可能是一个不错的候选者。

于 2014-07-14T21:57:45.673 回答