Skip to content

Latest commit

 

History

History
79 lines (48 loc) · 3.94 KB

Datasources.md

File metadata and controls

79 lines (48 loc) · 3.94 KB

Datasources

MemoryStorage

By default, DTTableViewManager uses MemoryStorage - a storage wrapper, representing storage of data models in memory.

manager.memoryStorage.setItems([1,2,3])

MemoryStorage has a lot of methods, allowing you to modify storage contents - adding, inserting, removing, replacing, moving, searching items in storage. For a complete list, head on to MemoryStorage documentation.

Diffable datasources (iOS/tvOS 13+)

Setting up diffable datasources is similar to how you would setup them without DTTableViewManager. The only difference is that instead of creating diffable datasource object using it's initializer, you instead use method provided by DTTableViewManager:

dataSource = manager.configureDiffableDataSource { indexPath, model in
   model
}

You can find working example of multi-section diffable datasources integration here.

More documentation on diffable datasources integration can be found here.

SingleSectionEquatableStorage

SingleSectionEquatableStorage is a storage for a single section, that calculates UI updates using provided differ.

No differ is provided by DTModelStorage, but you really need to build a thin adapter(5-6 lines of code) between your differ of choice and DTModelStorage for update calculation. DTModelStorage provides example of already built adapters for Dwifft, HeckelDiff and Changeset differs.

Setting up this kind of storage is simple:

let storage = SingleSectionEquatableStorage(items: arrayOfPosts, differ: ChangesetDiffer())
storage.setItems(startingItems)
manager.storage = storage

storage.addItems(newItems)

For more specific documentation on this storage, head on to this document.

CoreDataStorage

CoreDataStorage is designed to work with NSFetchedResultsController, and automatically animate all changes happening in the database.

manager.storage = CoreDataStorage(fetchedResultsController: controller)

For more information, read documentation on CoreDataStorage.

RealmStorage

RealmStorage is a single-section storage for data models from Realm

let results = try! Realm().objects(Dog)

let storage = RealmStorage()
storage.addSection(with:results)

For more details, please read documentation on RealmStorage.

Custom storage

If you are not happy with provided options, you can build custom storage, that fits your needs. You can either subclass any of 5 storages described above, or implement your own. The only requirement is Storage protocol. Additionally, you may implement SupplementaryStorage protocol, that allows storage to implement supplementary models / header-footer models.

For convenience, DTTableViewManager provides optional access to supplementaryStorage:

manager.supplementaryStorage?.setSectionHeaderModels([1])

Please note, that this accessor works also for any of the 5 storages described above, as well as for any storage, that implements SupplementaryStorage protocol.