簡體   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