Home > Software engineering >  is hashValue of Task unique on Swift concurrency?
is hashValue of Task unique on Swift concurrency?

Time:12-15

I would like to have an array of SomeProtocol which has a get property represents in Task extension.

If hashValue of Task is unique, I would like to consider it as a primary key to remove object in SomeProtocol array. And I wonder it is guaranteed on memory level, too.

Here's code below.

protocol SomeProtocol {
    var taskId: Int { get }
}


extension Task: SomeProtocol {
    var taskId: Int {
        get { return hashValue }
    }
}

CodePudding user response:

By definition, a hash value is not unique. If you want to assign a unique ID to your tasks, you will probably need to handle it yourself:

struct IdentifiableTask<E,F>: Identifiable where F:Error {
    let id = UUID()
    let task: Task<E,F>
    
    init(_ task: Task<E,F>) {
        self.task = task
    }
}
  • Related