supotuco (32) [Avatar] Offline
#1
Line 62 of BooksCollectionViewController.swift is

viewController.book = self.fetchedResultsController.object(at: selectedIndexPath)


suggested fix is

(edit)
viewController.book = self.fetchedResultsController.sections?[selectedIndexPath.section - 1].objects?[selectedIndexPath.row] as? Book
Craig Grummitt (49) [Avatar] Offline
#2
Hi supotuco, thanks again for your feedback. I assume you're talking about page 332, right? I'm squinting my eyes at your suggestion and I can't work it out! smilie Am I missing something? Aren't both lines the same?
supotuco (32) [Avatar] Offline
#3
I did mistype the suggestion.

it was supposed to be

viewController.book = self.fetchedResultsController.sections?[selectedIndexPath.section - 1].objects?[selectedIndexPath.row] as? Book


updated original comment
Craig Grummitt (49) [Avatar] Offline
#4
Hi supotuco - thanks for updating your question. Could I ask your reasoning behind the suggestion? Is the line in the book not working?
supotuco (32) [Avatar] Offline
#5
I get a crash when I select a book in the BookCollectionViewController
Craig Grummitt (49) [Avatar] Offline
#6
Ah ok. What sort of error messages are you seeing in the console?
Is it crashing on that line?
supotuco (32) [Avatar] Offline
#7
I don't know how to use the xcode stack trace to find the line so I attached my console output.

Steps to reproduce.

1) Checkout Chapter11.7.StoreCoreData branch from book repo
2) Add a book
3) Switch to collection view
4) Click on a book
Craig Grummitt (49) [Avatar] Offline
#8
I've finally had a chance to have a proper look at this, thank you for your steps to reproduce, that cleared things up. You're right - there's a bug there when clicking on a book in the collection view in the core data section.

Your suggestion is definitely one way to solve it, digging the section out manually of the fetched results.

I solved this a different way, in the cellForItemAt method, generating an updated index path from the selectedIndexPath but forcing it to section 0, so I could still take advantage of the object:at method:

let updatedIndexPath = IndexPath(row: selectedIndexPath.row, section: 0)
viewController.book = self.fetchedResultsController.object(at: updatedIndexPath)


I'm updated the repo, thanks again for discovering this and letting me know!