So for testing purposes and following an online tutorial, I made an extremely simple app that Presents an action sheet with the ability to add one fruit.
Current model code:
enum Section {
case main
}
struct Fruit: Hashable {
let uuid = UUID()
var title: String
static func == (lhs: Fruit, rhs: Fruit) -> Bool {
return lhs.uuid == rhs.uuid
}
func hash(into hasher: inout Hasher) {
hasher.combine(uuid)
}
}
DataSource
dataSource = UITableViewDiffableDataSource(tableView: tableView, cellProvider: { tableView, indexPath, itemIdentifier in
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = itemIdentifier.title
return cell
})
Code that updates DataSource
@objc func didTapAdd() {
let actionSheet = UIAlertController(title: "Select Fruit", message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Fruit \(1)", style: .default, handler: { [weak self] _ in
let fruit = Fruit(title: "Fruit: \(1)")
self?.fruits.append(fruit)
self?.updateDataSource()
})) present(actionSheet, animated: true)
@objc func add2() {
let fruit = Fruit(title: "Fruit: \(1)")
if !fruits.contains(fruit) {
self.fruits.append(fruit)
self.updateDataSource()
}
}
func updateDataSource() {
var snapshot = NSDiffableDataSourceSnapshot<Section, Fruit>()
snapshot.appendSections([.main])
snapshot.appendItems(fruits)
dataSource.apply(snapshot, animatingDifferences: true)
}
So whats happening is once I call add2
the Fruit 1 is duplicated on the tableView, I need to ignore it if its the same object. How can I accomplish this?
this is a quick representation of what I am trying to do on my main app, I basically need to populate CollectionViewCells from data from an Endpoint and from in app logic. Thing is the API gets called on every viewDidLoad, so I need to somehow ignore duplicates. Thanks!