简体   繁体   中英

why does tableview taking so much time to update?

i currently have a UITableView that shows me a list of people with the particular events which has been derived from the facebook. I currently have no issues fetching and inserting in core data,but the amount of time it takes to update the tableview is probably around 12 secs

User first selects an event

Then enters the edit screen

changes the event type to anniversary(balloons)

I navigate the same way back home,but here lies the problem, my tableview updates to the anniversary type event after probably 12 seconds.why is there so much delay in updating the 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;
    });
});



}

loadAllEventz-method which is called on initWithNibName

-(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 Class is a NSManagedObject subclass

the tableview performance depend on the datasource methods.

in your code you implemented GCD but the running is done in the mainthread itself!!

Problem for beign slow is the image loading.Make use of GCD in insearting image or use classes like Asynchimageview /AFnetworking extention,SDWebimageView etc

Maybe I am looking at this all wrong, but when an event is updated, are you sending the update to the web-service and then retrieving from the web-service when you go back to the TableView ? If so, that seems highly inefficient. Since you know the data that has changed, why not use a delegate method (or unwind segue ) to send the update back to the TableView and then just send the update to the web-service in the background? Unless you are needing information that you don't already have, there shouldn't be a reason to poll the web-service again. From looking at your flow, it seems that you are re-polling the web-service to get an update that you already know about.

Also, on the images. It sure looks like a small static group of images you are using for the reminders. Why not download them once (upon initial load of app) and then use the ones stored locally so you are not fetching them each time.

UITableView works fast. The problem isn't in tableView. You'd better measure elapsed time on yours "performing async operation".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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