简体   繁体   中英

is hashValue of Task unique on Swift concurrency?

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 }
    }
}

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM