Beginning Core Data 路 Fetched Results Controller | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/7104-beginning-core-data/lessons/14

Hi @bdmoakley - really enjoying this video tutorial. A couple questions:

Fetching objects
In MainViewController.swift, collectionView(_:numberOfItemsInSection) will the following statement cause all objects to be fetched from Core Data, which I don鈥檛 think we want?

let count = fetchedRC.fetchedObjects?.count ?? 0

Would it be better to use:
let count = fetchedRC.sections?[section].numberOfObjects ?? 0

SwiftUI

Is NSFetchedResultsController still useful with SwiftUI?

Can you point to any good examples of using NSFetchedResultsController with SwiftUI?

SwiftUI appears to re-use cells (with List) from what I鈥檝e read, but there is no mechanism to tell ForEach how many objects exist - it just expects a RandomAccessCollection. Similar to the concern of the first question, I don鈥檛 think we want to give ForEach this using fetchedObjects.

Perhaps there is some way to provide a lightweight dummy collection with the same number of objects and fetch from Core Data using object(at:) as needed?:

It鈥檚 been a long time since I worked with this, but the best bet is to do some of your own testing to see the results. Per the documentation, the sections property is really meant to be used with tableviews. It makes no reference with collection views.

As for SwiftuI, SwiftUI handles a lot of the Core Data calls via. property wrappers and the like. We鈥檙e in the process of updating this course to work with SwiftUI.