Search code examples
iosswiftcore-datansfetchedresultscontroller

NSFetchedResultsController per cell in a collectionView


I have a collectionView with NSFetchedResultsController. Some cells in the collectionView will have extra embedded UI elements queried from core data, and those extra embedded UI elements also need the update functionality of NSFetchedResultsController.

So my question is, what is the recommended way of approaching this?

Since the number of cells, and whether or not each of them has embedded UI elements depends on the data actually fetched from server, we cannot use sectionKeyPath of NSFRC right?

EDIT: the extra UI elements are not the same model as the embedding cells and thus require separate queries (NSPredicate).

EDIT: Our core data model:

RelationModel
   type
   status
   Relationships(fromProfile, toProfile)

ProfileModel
   ..many fields
   Relationships(photos)

Basically, the extra UI elements will be toProfiles with the embedding cell being the fromProfile. But because there are more than one kind of relations in the app, we decided to have a separate model for relations. And I found it hard to set a relationship from ProfileModel to the RelationModel


Solution

  • Since we require separate queries in each cell, we ended up setting separate NSFetchedResultsController in each cell when it's dequeued. And then have the NSFetchedResultsController set as nil when prepareForReuse.