3

當滾動或當viewDidAppear時,UITableViewController凍結:。我發現當問題開始時,應用程序開始無法控制地分配對象。 我有以下項目iOS:基本上從CoreData下載信息,但是當我去顯示錶中的數據時,應用程序凍結(設備和模擬器)。但如果我在下載的NSLog中顯示安排沒有錯誤。代碼如下:UITableViewController應用程序崩潰

I see that when the problem starts, the app start to allocate objects uncontrollably.

The UITableViewController freezes when scrolling or when 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; 
} 
+0

稍微OT:但如果你閱讀文檔,你應該**不**檢查錯誤!= nil'但檢查'返回== nil'。 '-executeFetchRequest:error:'does ** not **承諾任何'error'的值,除非'returned == nil'。 –

回答

0

在您的viewDidLoad中,您正在鎖定上下文[[core context] lock];,這導致您的getAllClassesUsingSortDescriptions等待鎖定清除。取下鎖。

+0

我做了你所說的,並沒有奏效,在Instruments看來,我發現當問題開始時,應用程序開始不受控制地分配對象。謝謝你的回答 –