Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Swift 5.1 collection diffing in internal type

Tags:

ios

swift

I'm building an iOS app that displays ranged iBeacons in a TableViewController.

To improve performances and test the new Swift 5.1 diffing feature I wrote the following code:

private func updateBeacons(_ rangedBeacons: [CLBeacon]) {
    guard beacons != rangedBeacons else { return }

    let difference = rangedBeacons.difference(from: beacons)
    // Also tried:
    // let difference = rangedBeacons.difference(from: beacons, by: { $0.uuid == $1.uuid })

    // ...
}

When this code is reached a fatalError is thrown:

Fatal error: unsupported: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1100.2.259.70/swift/stdlib/public/core/ArrayBuffer.swift, line 231

How can I perform collection diffing on CLBeacons?

The referenced code can be found here: https://github.com/apple/swift/blob/master/stdlib/public/core/ArrayBuffer.swift#L226-L232

like image 336
Emilio Schepis Avatar asked Oct 14 '19 10:10

Emilio Schepis


1 Answers

I was hitting this issue as well, but in my case I was getting one of the arrays from a Core Data NSFetchedResultsController. I suspect it's related to the fact that the original array comes from Objective-C.

I was able to fix this by wrapping the arrays in a new Array:

private func updateBeacons(_ rangedBeacons: [CLBeacon]) {
    guard beacons != rangedBeacons else { return }

    let difference = Array(rangedBeacons).difference(from: Array(beacons))

    // ...
}
like image 67
Matt Moriarity Avatar answered Nov 11 '22 19:11

Matt Moriarity