繁体   English   中英

为什么tableview需要这么多时间来更新?

[英]why does tableview taking so much time to update?

我目前有一个UITableView,它向我显示了具有来自Facebook的特定事件的人员列表。 我目前在获取和插入核心数据方面没有任何问题,但是更新表视图所需的时间可能约为12秒

用户首先选择一个事件

然后进入编辑屏幕

将事件类型更改为周年纪念日(气球)

我以相同的方式回到家,但问题就出在这里,我的表视图可能在12秒后更新为周年纪念类型的事件。为什么更新表视图的延迟如此之长

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *CellIdentifier = @"EventCell";

//testing


EventCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PersonEvent *currentEvent = [self.eventsArray objectAtIndex:indexPath.row];

[cell configureForEvent:currentEvent];
//  NSLog(@"Event date is %@",[currentEvent.eventDate description]);
// NSLog(@"Time interval %f",[currentEvent timeDifference]);
//[self configureCell:cell atIndexPath:indexPath];
return cell;

}

configureForEvent

-(void)configureForEvent:(PersonEvent*)theEvent
 {

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
    //performing async operation her

    dispatch_sync(dispatch_get_main_queue(), ^{

        personLabel.text = theEvent.name;
        dateLabel.text = [theEvent getFriendlyDate];
        //  NSInteger theAge = [theEvent getAge];
        if (theEvent.hasAge) {
            //  NSLog(@"Have to load the age as well for %@",theEvent.name);
            ageLabel.text = [NSString stringWithFormat:@"%d yrs",[theEvent getAge] ];
        }
        else{
            ageLabel.text = @"";
        }

        //ageLabel.text = [NSString stringWithFormat:@"%d yrs",[theEvent getAge] ];
        reminderImage.image = [theEvent.theReminders count] == 0?[UIImage imageNamed:@"reminder.png"]:nil;
        // Have to configure event type image based on the event type..
        //NSLog(@"Event image %@",[eventTypeImagesArray objectAtIndex:theEvent.eventType]);
        eventTypeImage.image = [UIImage imageNamed:[eventTypeImagesArray objectAtIndex:theEvent.eventType]];;

        // Update UI
        // Example:
        // self.myLabel.text = result;
    });
});



}

在initWithNibName上调用的loadAllEventz方法

-(void)loadAllEventz
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"PersonEvent" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"eventDate" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor]];

NSError *error;
    allEventsArray = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(@"%@",allEventsArray); 
//  [appDelegate.managedObjectContext executeFetchRequestAsynchronously:fetchRequest delegate:self];


 }

PersonEvent类是NSManagedObject的子类

tableview的性能取决于数据源方法。

在您的代码中,您实现了GCD,但运行是在主线程本身中完成的!

图像加载缓慢的问题是图像加载。在插入图像时使用GCD或使用Asynchimageview / AFnetworking扩展,SDWebimageView等类

也许我看错了所有,但是当事件被更新时,您是否将更新发送到Web服务,然后在返回TableView时从Web服务检索? 如果是这样,那似乎效率很低。 既然您知道更改的数据,为什么不使用delegate方法(或unwind segue )将更新发送回TableView ,然后仅在后台将更新发送到Web服务? 除非您需要的信息不存在,否则没有理由再次轮询该Web服务。 通过查看流程,您似乎正在重新轮询Web服务以获得已知的更新。

另外,在图像上。 它肯定看起来像是用于提醒的一小组静态图像。 为什么不下载一次(在应用程序初始加载时),然后使用本地存储的文件,这样就不必每次都获取它们。

UITableView可以快速运行。 问题不在tableView中。 您最好根据自己的“执行异步操作”来衡量经过时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM