realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.32k stars 2.15k forks source link

[Feature Request] Suitable RealmCollectionChange API for NSDIffableDataSourceSnapshot #7617

Open mtgto opened 2 years ago

mtgto commented 2 years ago

Problem

RealmSwift provides RealmCollectionChange to listen a write transaction for realm collection. https://docs.mongodb.com/realm/sdk/swift/examples/react-to-changes/#register-a-collection-change-listener It makes enable partial update TableView and CollectionView easily.

Since iOS 13+, Apple releases NSDIffableDataSourceSnapshot to provide diff data for collection view and table view.

NSDIffableDataSourceSnapshot is different API from UITableView/UICollectionView to partial update. NSDIffableDataSourceSnapshot is based on Identifier which items are added/deleted/updated. (See Apple's API documentation and Sample application for details)

However, RealmCollectionChange only give me which indexes are added or deleted or updated. I wanna identifier of item is deleted from callback of RealmCollectionChange, but there are only indexes. So I can't find which items are deleted.

Note: I can find which items are added/updated by indexes. I can access added/updated items referring realm collection via added/updated indexes.

Solution

It helps me Realm gives the way to retrieve which item or identifier of item are deleted from realm collection.

API usage image: (Not working now)

let results = realm.objects(Dog.self)

notificationToken = results.observe { [weak self] changes in
    guard let tableView = self?.tableView else { return }
    var snapshot = tableView.dataSource.snapshot()
    switch changes {
    case .initial:
        var snapshot = NSDiffableDataSourceSnapshot<Int, String>()
        snapshot.appendSections([0])
        snapshot.appendItems(results.map { $0.id })
        tableView.dataSource.apply(snapshot)
    case .update(_, let deletions, let insertions, let modifications):
        var snapshot = tableView.dataSource.snapshot()
        snapshot.deleteItems(deletions)
        snapshot.appendItems(insertions)
        snapshot.reloadItems(modifications)
        tableView.dataSource.apply(snapshot)

Only identifier is needed for deletions and modifications. Both Identifier and insertion position are needed for insertions.

How important is this improvement for you?

I'd like to see it, but have a workaround

c128128 commented 2 years ago

Any news on this ?

c128128 commented 2 years ago

We really need the concrete Object that was deleted, changed in RealmCollectionChange and not only the indexes. Or maybe the _id (primary key) of the object.

AF-cgi commented 2 years ago

@mtgto Can you post your workaround? I'm pretty interested on it.

mtgto commented 2 years ago

I have no workaround yet. Always reload data even if it does not need..

matijag1 commented 8 months ago

Are there any news regarding this issue?