I'm trying to prevent a user from adding a duplicate name into a Core Data entity. I have a constraint setup for the name but I still need to do the duplicate validation before saving the data. I have a record with the name of 'Test' but I'm still allowed to add a new record with the name of 'test', the only difference being the case sensitivity between the two names is different.
When I perform a fetch request before saving to determine if the record already exists, I'm adding a case insensitive sort descriptor:
let fetchRequest: NSFetchRequest<ItemStore> = ItemStore.fetchRequest()
let storeName: String = storeNameText.text!
fetchRequest.predicate = NSPredicate(format: "%K = %@", argumentArray: [#keyPath(ItemStore.name), storeName])
let sortDescriptor = NSSortDescriptor(key: #keyPath(ItemStore.name), ascending: true, selector: #selector(NSString.caseInsensitiveCompare))
fetchRequest.sortDescriptors = [sortDescriptor]
But the fetch request doesn't appear to recognize that selector. If I pass 'test' as the storeName into the fetch request, the record for 'Test' isn't being returned.
Is there something simple I'm overlooking here, or is there a better way to check for duplicate records?