我目前有一个 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;
}
配置事件
-(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 的子类