Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Fatal error: Duplicate keys of type 'AnyHashable2' were found in a Dictionary.

I have encountered the following error and reduced my code to the minimum necessary to reliably reproduce this error.

Fatal error: Duplicate keys of type 'AnyHashable2' were found in a >Dictionary. This usually means either that the type violates Hashable's >requirements, or that members of such a dictionary were mutated after insertion.

It occurs when

  • instances of a swiftdata model are inserted (the error occurs reliably when inserting five or more instances. Fewer insertions seems to make the error either more rare or go away entirely) and
  • a Picker with .menu pickerStyle is present.

Any of the following changes prevents the error from occuring:

  • adding id = UUID() to the Item class
  • removing .tag(item) in the picker content
  • using any pickerStyle other than .menu
  • using an observable class instead of a swiftdata class

I would greatly appreciate if anyone knows what exactly is going on here.

Tested using XCode Version 16.4 (16F6), iPhone 16 Pro iOS 18.5 Simulator and iPhone 15 Pro iOS 18.5 real device.

import SwiftUI
import SwiftData

@Model class Item {
   var name: String
    
    init(name: String) {
        self.name = name
    }
}

struct DuplicateKeysErrorView: View {
    
    @Environment(\.modelContext) private var modelContext
    @Query(sort: \Item.name) private var items: [Item]
    
    @State var selection: Item? = nil
    
    var body: some View {
        List {
            Picker("Picker", selection: $selection) {
                Text("Nil").tag(nil as Item?)
                ForEach(items) { item in
                    Text(item.name).tag(item)
                }
            }
            .pickerStyle(.menu)
            Button("Add 5 items") {
                modelContext.insert(Item(name: UUID().uuidString))
                modelContext.insert(Item(name: UUID().uuidString))
                modelContext.insert(Item(name: UUID().uuidString))
                modelContext.insert(Item(name: UUID().uuidString))
                modelContext.insert(Item(name: UUID().uuidString))
            }
        }
        .onAppear {
            try! modelContext.delete(model: Item.self)
        }
    }
}

#Preview {
    DuplicateKeysErrorView()
        .modelContainer(for: Item.self)
}
Answered by joadan in 842222022

When a model object is saved its identifier is changed from a temporary one to a permanent one (so I assume the hash values also changes) so this is most likely what cases an issue here with the picker.

One way to work around this is to call save() directly after the inserts.

Accepted Answer

When a model object is saved its identifier is changed from a temporary one to a permanent one (so I assume the hash values also changes) so this is most likely what cases an issue here with the picker.

One way to work around this is to call save() directly after the inserts.

Fatal error: Duplicate keys of type 'AnyHashable2' were found in a Dictionary.
 
 
Q