Iglistkit: Reusing ListAdapter with a different dataSource

Created on 20 Sep 2017  路  2Comments  路  Source: Instagram/IGListKit

New issue checklist

General information

  • IGListKit version: 3.0
  • iOS version(s): 11
  • CocoaPods/Carthage version:
  • Xcode version: 9
  • Devices/Simulators affected: YES
  • Reproducible in the demo project? YES
  • Related issues: None

My use case:

I essentially have 2 feeds (Feed 1 & Feed 2) however each feed has some common sections and I want transition smoothly between them. In order to do this I am using the same collectionView and same listAdapter for both of them. I pass reference and reuse the adapter from Feed 1 to Feed 2, all I do is set the adapter's dataSource = Feed 2. However, when I do this, the adapter resets the sectionMap which is unfortunate because the next time I call performBatchUpdates with the data from Feed 2 it crashes with NSInternalInconsistencyException (Wrong number of sections before/after), because the sectionMap no longer carries the data from the previous feed.

I've created a fix for this, but not sure if this jives well with Instagram's use case:

- (void)setDataSource:(id<IGListAdapterDataSource>)dataSource {
    if (_dataSource != dataSource) {

        BOOL shouldUpdate = false;
        if (_dataSource == nil) {
            shouldUpdate = true;
        }

        _dataSource = dataSource;

        if(shouldUpdate) {
            [self updateAfterPublicSettingsChange];
        }
    }
}

This only resets the section map if the dataSource was nil before.

What do you think?

question

Most helpful comment

@rnystrom ahh you are right, I can create a higher level object and swap between my two sources pretty easily...

All 2 comments

@yusufoos I wouldn't change the data source, IGListAdapter isn't designed to do that, as you'd discovered. Your patch could work, but I'm not sure it's something we want to encourage.

Why can't you have a single data source and _within_ the data source fork between two sources that return stuff for objects(for listAdapter:)?

I'm also curious what separating data sources unlocks for you? Are you reusing the data sources elsewhere?

@rnystrom ahh you are right, I can create a higher level object and swap between my two sources pretty easily...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rnystrom picture rnystrom  路  3Comments

alexwillrock picture alexwillrock  路  3Comments

runmad picture runmad  路  3Comments

shuhrat10 picture shuhrat10  路  3Comments

kleiberjp picture kleiberjp  路  3Comments