3

UITableViewController滚动或时冻结:viewDidAppear。我看到当问题开始时,应用程序开始无法控制地分配对象。我有以下项目iOS:基本上从 下载信息CoreData,但是当我去显示表中的数据时,应用程序冻结(设备和模拟器)。但是如果我在下载的时候显示排列NSLog没有错误。代码如下:

我看到当问题开始时,应用程序开始无法控制地分配对象。

UITableViewController 在滚动或 viewDidAppear 时冻结:

表视图控制器.h

@interface Set_ScheduleViewController : UITableViewController
{
    STCore *core;
    UILabel *label;
    NSArray *objects;
}
@end

表视图控制器.m:

@implementation Set_ScheduleViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    core = [[STCore alloc] init];
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)reloadData
{
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    objects = [core getAllClassesUsingSortDescriptions:@[sort]];
    [[self tableView] reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self reloadData];
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [objects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 20)];
        label.textColor = [UIColor colorWithHex:0x88B6DB];
        label.font = [UIFont systemFontOfSize:12.0f];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = UITextAlignmentRight;
        label.minimumFontSize = 12.0f;
        
        tableView.separatorColor = [UIColor colorWithHex:0xDAE1E6];
        
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor colorWithHex:0x393B40];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
        
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.textColor = [UIColor colorWithHex:0x393B40];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
    }
    
    cell.textLabel.text = [[objects objectAtIndex:indexPath.row] name];
    cell.detailTextLabel.text = [[objects objectAtIndex:indexPath.row] location];
    
    STMultipleDate *dates = [STMultipleDate new];
    for (Schedule *sch in [[objects objectAtIndex:indexPath.row] schedule]) {
        STDate *date = [STDate new];
        [date setWeekday:[[sch day] integerValue]];
        [date setHour:[[sch hour] integerValue]];
        [date setMinutes:[[sch minutes] integerValue]];
        
        [dates addDate:date];
    }
    
    label.text = [STCore splitDates:dates];
    cell.accessoryView = label;
    return cell;
}


- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if ([objects count] < 1) {
        return NSLocalizedString(@"SETSCH_FOOTER_DEFAULT_404", @"");
    }
    return nil;
}

STCore.m

- (NSArray *)getAllClassesUsingSortDescriptions:(NSArray *)descs
{
    if (![self isReady]) {
        return @[];
    }
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Classes" inManagedObjectContext:_context];
    
    [fetchRequest setEntity:entity];
    [fetchRequest setSortDescriptors:descs];
    
    NSError *error;
    NSArray *returned = [_context executeFetchRequest:fetchRequest error:&error];
    
    if (error) {
        [_delegate core:self didRecivedAnError:error];
    }
    
    return returned;
}
4

1 回答 1

0

在您viewDidLoad中,您使用 锁定上下文[[core context] lock];,这导致您getAllClassesUsingSortDescriptions等待锁定清除。取下锁。

于 2013-04-01T02:25:50.117 回答