我在这里遵循了这个教程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]];
}
答案 0 :(得分:1)
是的,您无法按原样提高速度......客户端不应该首先下载每个对象,滚动列表包含500多个项目并不是一个好的用户体验。也许你应该有一个初始屏幕,他们选择一些子集,然后他们可以在下一个屏幕上查询一小组数据。您目前正在使用的部分可能是一个很好的候选人。