swiftmacosnstableviewdiffabledatasourcensdiffabledatasourcesnapshot

How to Use NSTableViewDiffableDataSource to Load Data with NSTableView


I'm trying to learn how to use NSTableViewDiffableDataSource to load data with NSTableView. I am able to use UITableViewDiffableDataSource and UICollectionViewDiffableDataSource to load data in iOS because I have found some examples online. But I am not able to use NSTableViewDiffableDataSource in Cocoa.

In the following case, I have a subclass of NSTableCellView named TestTableCellView, which shows three fields: First name, Last name, and his or her date of birth in String.

import Cocoa

class ViewController: NSViewController {
    // MARK: - Variables
    var dataSource: NSTableViewDiffableDataSource<Int, Contact>?
    
    
    // MARK: - IBOutlet
    @IBOutlet weak var tableView: NSTableView!
    
    
    // MARK: - Life cycle
    override func viewWillAppear() {
        super.viewWillAppear()
        
        let model1 = Contact(id: 1, firstName: "Christopher", lastName: "Wilson", dateOfBirth: "06-02-2001")
        let model2 = Contact(id: 2, firstName: "Jen", lastName: "Psaki", dateOfBirth: "08-25-1995")
        let model3 = Contact(id: 3, firstName: "Pete", lastName: "Marovich", dateOfBirth: "12-12-2012")
        let model4 = Contact(id: 4, firstName: "Deborah", lastName: "Mynatt", dateOfBirth: "11-08-1999")
        let model5 = Contact(id: 5, firstName: "Christof", lastName: "Kreb", dateOfBirth: "01-01-2001")
        let models =  [model1, model2, model3, model4, model5]
        
        dataSource = NSTableViewDiffableDataSource(tableView: tableView, cellProvider: { tableView, tableColumn, row, identifier in
            let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cell"), owner: self) as! TestTableCellView
            let model = models[row]
            cell.firstField.stringValue = model.firstName
            cell.lastField.stringValue = model.lastName
            cell.dobField.stringValue = model.dateOfBirth
            return cell
        })
        tableView.dataSource = dataSource
        guard let dataSource = self.dataSource else {
            return
        }
        var snapshot = dataSource.snapshot()
        snapshot.appendSections([0])
        snapshot.appendItems(models, toSection: 0)
        dataSource.apply(snapshot, animatingDifferences: true, completion: nil) // <--- crashing...
    }
}

struct Contact: Hashable {
    var id: Int
    var firstName: String
    var lastName: String
    var dateOfBirth: String
}

Hmm... The application crashes with an error "Invalid parameter not satisfying: snapshot." A couple of days ago, I tested another example, which also crashed at the same line (dataSource.apply). I don't find many examples involving NSTableViewDiffableDataSource online. The only example I have found is this topic, which doesn't help. Anyway, what am I doing wrong? My Xcode version is 13.1. Thanks.


Solution

  • Create a snapshot like this and it should work:

    guard let dataSource = self.dataSource else {
        return
    }
    
    var snapshot = NSDiffableDataSourceSnapshot<Int, Contact>()
    snapshot.appendSections([0])
    snapshot.appendItems(models, toSection: 0)
    dataSource.apply(snapshot, animatingDifferences: false)
    

    enter image description here