8

Ho ottenuto un risultato con una singola sezione. Sono in grado di accedere agli oggetti utilizzando [[fetchedResultsController fetchedObjects] objectAtIndex:index] per tutti gli oggetti. Ma fallisce quando uso objectAtIndexPath in questo modo: [fetchedResultsController objectAtIndexpath:indexPath]NSFetchedResultsController objectAtIndex, objectAtIndexPath, indexPathForObject incoerenze

Un errore si verifica dopo aver inserito una riga (per uno degli oggetti SearchResult) nella tabella corrispondente. L'oggetto sembra essere inserito correttamente nella nuova tabella. Dopo aver verificato visivamente questo aspetto, seleziono una delle righe, quindi inizia il divertimento.

Ecco il codice in cui si verifica l'errore:

- (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]; 
} 

Sto avendo un fallimento nell'ultima riga. Il registro è simile al seguente:

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] 

Dopo l'esecuzione del bilancio NSLog, ottengo un'eccezione all'ultimo line utilizzando [fetchedResultsController objectAtIndexPath:indexPath]. Succede anche per altri valori di indice, ma sembrano sempre validi.

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

Quindi, per riassumere, sembra che ci siano il giusto numero di oggetti recuperati, v'è una sezione (0), posso accedere ognuno con un metodo, ma non dagli altri. IndexPathForObject: restituisce sempre (null).

È un errore o sto fraintendendo qualcosa?


UPDATE

Ecco il codice, implementare i metodi di protocollo 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

Non so se è importante, ma il tableView viene inizializzato con questa linea:

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

UPDATE 3

Quando cambio la riga incriminata come segue, funziona va bene Ma preferirei mantenere l'indicizzazione come con il tavolo.

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

come hai inserito l'oggetto? Puoi pubblicare il codice in cui aggiungi l'oggetto al tuo archivio dati principale e aggiungi la riga al tuo TableView? – timthetoolman

+0

aggiornato. Vedi sopra. – Jim

+0

stai aggiungendo qualcosa al Core Data Store o semplicemente aggiungendo una riga al TableView? – timthetoolman

risposta

0

Ho avuto lo stesso problema ora.

Per me è stato utile inizializzare NSFetchedResultsController con cacheName: nil.

Problemi correlati