为什么tableview花了这么多时间来更新?

时间:2013-05-14 09:46:41

标签: ios objective-c uitableview core-data

我目前有一个UITableView,它向我展示了一个人的列表,其中包含从facebook派生的特定事件。 我目前在核心数据中获取和插入没有问题,但更新tableview所需的时间可能大约为12秒

用户首先选择一个事件

然后进入编辑屏幕

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

我以相同的方式回到家里,但这就是问题, 我的tableview在大概12秒之后更新了周年纪念类型事件。为什么更新tableview会有太多延迟

的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-method

-(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子类

3 个答案:

答案 0 :(得分:0)

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

在你的代码中你实现了GCD,但是运行是在mainthread本身完成的!!

beign slow的问题是图像加载。使用GCD处理图像或使用Asynchimageview / AFnetworking extention,SDWebimageView等类

答案 1 :(得分:0)

也许我看到这一切都错了,但是当事件更新时,您是否将更新发送到网络服务,然后在返回TableView时从网络服务中检索?如果是这样,那似乎非常低效。由于您知道已更改的数据,为什么不使用delegate方法(或unwind segue)将更新发送回TableView,然后将更新发送到Web服务在后台?除非您需要您尚未拥有的信息,否则没有理由再次轮询Web服务。通过查看您的流程,您似乎正在重新轮询Web服务以获取您已经了解的更新。

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

答案 2 :(得分:0)

UITableView运行速度很快。问题不在tableView中。您最好测量您的“执行异步操作”所用的时间。