UITableViewController应用程序崩溃

时间:2013-04-01 02:01:34

标签: objective-c core-data uitabbarcontroller nsmanagedobject freeze

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

  

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

     

UITableViewController在滚动或viewDidAppear时冻结:

TableViewController.h

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

TableViewController.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;
}

1 个答案:

答案 0 :(得分:0)

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