0

안녕하세요. NSFetchedResultsController가 3.1 SDK에서 제대로 작동하지만, 시도 할 때 대리자 메서드에서 이상한 오류가 발생하기 시작합니다. 3.0 이하. NSFetchedResultsControllerDelegate 메서드와 관련이 있다고 판단했습니다. 이것이 내가 설정 한 것입니다.NSFetchedResultsController - iPhone OS 3.0에서 충돌하는 대리자 메서드 3.1 미만이 아닙니다.

inEditingMode 항목은 테이블에 다른 정적 섹션을 추가하여 구현 한 방식과 관련이 있습니다.

Serious application error. Exception was caught during Core Data change processing: *** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null) 

내가 objc_exception_throw에 중단 점을 넣어 충돌은 controllerDidChangeContent의 내부에서 발생하는 것 같다 : I 관리되는 개체 컨텍스트에 엔티티를 추가 할 때

- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller { 
    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{ 
    NSIndexSet *sectionSet = [NSIndexSet indexSetWithIndex:sectionIndex]; 

    if(self.inEditingMode){ 
     sectionSet = [NSIndexSet indexSetWithIndex:sectionIndex + 1]; 
    } 

    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:sectionSet withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:sectionSet withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     default: 
      [self.tableView reloadData]; 
      break; 

    } 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{ 
    NSIndexPath *relativeIndexPath = indexPath; 
    NSIndexPath *relativeNewIndexPath = newIndexPath; 

    if(self.inEditingMode){ 
     relativeIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section + 1]; 
     relativeNewIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row inSection:newIndexPath.section + 1]; 
    } 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:relativeNewIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:relativeIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     default: 
      [self.tableView reloadData]; 
      break; 
    } 
} 


-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{ 
    [self.tableView endUpdates]; 
} 

, 나는 다음과 같은 오류가 발생합니다.

모든 self.tableView 메서드를 주석 처리하고 controllerDidChangeContent 내에 [self.tableView reloadData]를 하나만 넣으면 모든 것이 예상대로 작동합니다.

아무도 이런 일이 발생하는 이유에 대해 알고 있습니까?

답변

2

NSFetchedResultsController의 설명서에는 컨트롤러에서보고 한 섹션 수와 UITableView에서 예상되는 섹션 수 사이의 불일치로 이어지는 3.0 구현의 버그에 대한 구체적인 언급이 있습니다. 이것은 그들이 제공하는 해결 방법입니다 :이 해결 방법은 OS 3.1에 필요없는 것이

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    NSUInteger count = [[<#Fetched results controller#> sections] count]; 
    if (count == 0) { 
     count = 1; 
    } 
    return count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    NSArray *sections = [<#Fetched results controller#> sections]; 
    NSUInteger count = 0; 
    if ([sections count]) { 
     id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section]; 
     count = [sectionInfo numberOfObjects]; 
    } 
    return count; 
} 

주, 그래서 당신은 오류가 표시되지 않는 이유를 설명 할 수있다. 해결 방법은 sectionNameKeyPath가 nil로 설정된 경우 3.0에서만 필요합니다. sectionNameKeyPath에 값을 설정하면 문제가되지 않습니다.

+0

내가 처음 구현 한 것을 추가하는 것을 잊어 버렸습니다. 이미 구현했지만 문제는 여전히 지속되었습니다. –

+0

실제로 자세히 조사하고이 수정을 다시 구현하면 문제가 사라집니다. 나는 지난 번 이상한 일을 했음에 틀림 없다. 고마워! –

+0

다행스럽게 들었습니다. Core Data 및 NSFetchedResultsController 오류는 진단하고 수정하는 것이 매우 까다로울 수 있습니다. – glorifiedHacker

관련 문제