Is there more efficient way to keep custom object array unique by each pagination data feeds?

88 Views Asked by At

In the tableView's willDisplay checks latest cell and then trigger new request for upcoming datas cited as pagination.

I want to keep that custom object array's IDs as unique with upcoming datas too.

I tried below solution to make it, but I wonder that Is there anything to do more efficiently?

let idSet = NSCountedSet(array: (newDatas + self.ourDatas).map { $0.id })
let arr = (newDatas + self.ourDatas).filter { idSet.count(for: $0.id) == 1 }
self.ourDatas = arr // ourDatas is dataSource of tableView
self.tableView.reloadData()

Also above way mixes all datas, how can I continue keeping it as ordered?

2

There are 2 best solutions below

2
On BEST ANSWER

You should keep two properties; your array of items (ourDatas) and a set of ids (var ourIds = Set<String>(). I am assuming your ids are Strings

You can do something like this

var insertedRows = [IndexPath]()
for newItem in newDatas {
   if !ourIds.contains(newItem.id) {
      insertedRows.append(IndexPath(item:self.ourDatas.count,section:0))
      ourIds.insert(newItem.id)
      ourDatas.append(newItem)
   }
}

if !insertedRows.empty {
   self.tableView.insertRows(at: insertedRows, with:.automatic)
}
0
On

I have a helper extension for Array for this. It "reduces" an array having only "distinct" elements:

public extension Array where Element: Identifiable {
    var distinct: [Element] {
        var elements = Set<Element.ID>()
        return filter { elements.insert($0.id).inserted }
    }
}

based on the element's id. That is, you need to have your elements conform to Identifiable - which is very useful anyway when showing them in a TableView or List.

So, when you receive a new array of elements, just do this:

    self.elements = (self.elements + newElements).distinct

A small test for Playgrounds:

extension Int: Identifiable {
    public var id: Int { self }
}

var a = [1,2,3,4,5]
var b = [1,2,3,6,7]

print((a + b).distinct)

Console:

[1, 2, 3, 4, 5, 6, 7]