somenickname
somenickname

Reputation: 619

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

Upvotes: 1

Views: 145

Answers (2)

somenickname
somenickname

Reputation: 619

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.

Upvotes: 0

Jon Rose
Jon Rose

Reputation: 8563

A NSFetchedResultsController is a really cool object. It does a fetch and then monitors core-data for changes. While it has an interface that relies on indexPaths so it is natural to think of these indexPath's as the same indexPaths as your collectionView there is no requirement that you do that. The indexPaths of the fetchedResultsController can be different than the indexPaths of the collectionView - you just need to careful about keeping track which indexPaths you are dealing with and translating from one to the other.

For example: You have a set of widgets that you fetching from core data. Some of the widgets have a property of extraWidgetInfo which you want to display in you UI as an extra cell. The fetchedResultsController says that there are 4 element (all in section 0). But the collectionView can display that as

[section1] widget1,

[section2] widget2, widget 2 extra info,

[section3] widget3,

[section4] widget4, widget 4 extra info.

While the fetchedResultsController only says that there are 4 elements, there are 6 cells in the collectionView. You would also have to translate the fetchedResultsController indexPath when dealing with updates. An update would translate to a reload section, and an add would translate to an insertSection and insert of some amount of rows in that section. You could also just call reloadData when core data updates (If you data is updating rarely this may not be a bad option).

Upvotes: 1

Related Questions