I’ve just started using Core and and I’m kind of stuck. I’ve a Book entity which has a category attribute of type String. The data is loaded into a collection view and grouped by based on this category. All’s working fine this way, but the problem is that a Book should be able to appear in multiple categories (and collection view sections). I could change the category’s type to an Array of String, but how would I do the sorting then?

let request = Book.fetchedRequest() as NSFetchRequest<Book>
let categorySort = NSSortDescriptor(: #keyPath(Book.category)), ascending:true)
request.sortDescriptors = [categorySort]

do {
fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: #keyPath(Book.category)), cacheName: nil) 
try fetchedResultsController.performFetch()
catch let error as NSError {
print(“Could not fetch. (error), (error.userInfo)”)

Should I just give up on using NSFetchedResultsController or is it possible to make this work?

Source link


Please enter your comment!
Please enter your name here