8

Tôi có kết quả tìm nạp với một phần duy nhất. Tôi có thể truy cập các đối tượng sử dụng [[fetchedResultsController fetchedObjects] objectAtIndex:index] cho tất cả các đối tượng. Nhưng nó không thành công khi tôi sử dụng objectAtIndexPath như thế này: [fetchedResultsController objectAtIndexpath:indexPath]NSFetchedResultsController objectAtIndex, objectAtIndexPath, indexPathForObject mâu thuẫn

Lỗi xảy ra sau khi tôi chèn một hàng (cho một trong các đối tượng SearchResult) vào bảng tương ứng. Đối tượng dường như được chèn vào bảng mới chính xác. Sau khi tôi đã xác nhận trực quan điều này, tôi chọn một trong các hàng, và sau đó vui vẻ bắt đầu.

Đây là mã nơi lỗi xảy ra:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SearchResult *event; 
    NSLog(@"Number of sections in fetchedResultsController: %d", [[fetchedResultsController sections] count]); 
    for (NSUInteger i=0; i<[[fetchedResultsController fetchedObjects] count]; i++) { 
     event = (SearchResult*)[[fetchedResultsController fetchedObjects] objectAtIndex:i]; 
     NSLog(@"object at index[%d]: %@", i, event.title); 
     NSLog(@"indexPath for object at index[%d]:%@", i, [fetchedResultsController indexPathForObject:event]); 
    } 

    NSLog(@"indexPath passed to method: %@", indexPath); 

    SearchResult *result = [fetchedResultsController objectAtIndexPath:indexPath]; // *** here is where the error occurs *** 

    [viewDelegate getDetails:result]; 
} 

Tôi gặp một thất bại trong dòng cuối cùng. Nhật ký trông giống như sau:

Number of sections in fetchedResultsController: 1 
object at index[0]: Cirles 
indexPath for object at index[0]:(null) 
object at index[1]: Begin 
indexPath for object at index[1]:(null) 
object at index[2]: Copy 
indexPath for object at index[2]:(null) 
object at index[3]: Unbound 
indexPath for object at index[3]:(null) 
indexPath passed to method: <NSIndexPath 0x64ddea0> 2 indexes [0, 2] 

Sau khi thực hiện báo cáo NSLog, tôi nhận được ngoại lệ ở dòng cuối cùng sử dụng [fetchedResultsController objectAtIndexPath:indexPath]. Nó cũng xảy ra với các giá trị chỉ mục khác, nhưng chúng luôn có giá trị.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 2 in section at index 0'

Vì vậy, để tóm tắt, có vẻ như là đúng số đối tượng lấy, có một phần (0), tôi có thể truy cập vào từng cái một phương pháp, nhưng không phải do khác. IndexPathForObject: luôn trả về (null).

Đây có phải là lỗi hoặc tôi hiểu nhầm điều gì đó không?


CẬP NHẬT

Đây là mã, thực hiện các phương pháp giao thức NSFetchedResultsControllerDelegate.

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

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    NSLog(@"Favorites Table controllerDidChangeContent"); 
    [self.tableView endUpdates]; 
} 

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

    NSLog(@"Favorites Changed Object"); 

    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      NSLog(@"--- Favorite was inserted"); 
      if ([[self.fetchedResultsController fetchedObjects] count] == 1) { 
       // configure first cell to replace the empty list indicator by first deleting the "empty" row 
       [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone]; 
      } 

      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone]; 

      break; 

     case NSFetchedResultsChangeDelete: 
      NSLog(@"--- Favorite was deleted"); 

      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
      if ([[self.fetchedResultsController fetchedObjects] count] == 0) { 
       // configure first cell to show that we have an empty list 
       [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
      } 

      break; 

     case NSFetchedResultsChangeMove: 
      NSLog(@"--- Favorite was moved"); 

      break; 

     case NSFetchedResultsChangeUpdate: 
      NSLog(@"--- Favorite was updated"); 
      [self configureCell:(ResultCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 

      break; 

     default: 
      break; 
    } 

} 

- (void)controller:(NSFetchedResultsController *)controller 
    didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex 
    forChangeType:(NSFetchedResultsChangeType)type { 

    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      NSLog(@"Favorites Section Added"); 

      break; 

     case NSFetchedResultsChangeDelete: 
      NSLog(@"Favorites Section Deleted"); 

      break; 

     default: 
      break; 
    } 

} 

UPDATE 2

Tôi không biết nếu nó quan trọng, nhưng tableView được khởi tạo với dòng này:

tableView = [[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStyleGrouped]; 

CẬP NHẬT 3

Khi tôi thay đổi dòng vi phạm như sau, nó hoạt động s tốt. Nhưng tôi thà giữ chỉ mục giống như với bảng.

//SearchResult *result = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
SearchResult *result = [[self.fetchedResultsController fetchedObjects] objectAtIndex:[indexPath indexAtPosition:1]] 
+0

bạn đã chèn đối tượng như thế nào? Bạn có thể đăng mã nơi bạn đang thêm đối tượng vào kho dữ liệu cốt lõi của mình và thêm hàng vào tableView của bạn không? – timthetoolman

+0

Đã cập nhật. Xem ở trên. – Jim

+0

là bạn có thực sự thêm gì đó vào Kho dữ liệu cốt lõi hay chỉ thêm một hàng vào tableView không? – timthetoolman

Trả lời

0

Tôi đã gặp sự cố tương tự ngay bây giờ.

Đối với tôi, nó đã giúp khởi tạo NSFetchedResultsController với cacheName: nil.

Các vấn đề liên quan