I’ve hit a bit of a dead end trying to use NSPredicate to return CoreData objects whose array property [String] contains a specific String (ie. the search query)
The CoreData objects being fetched are a custom class called ‘LogEntry’
Each ‘LogEntry’ has a property called ‘procedureTypes’ which is an array of strings - [String]
I’m trying to create a predicate for the fetch request that will return all objects that contain a specific String in the procedureTypes array but I can’t work out how to construct the predicate format.
I’ve tried
“procedureTypes CONTAINS %@”
…and various permutations of this to no avail. I just get crashes.
Searching for simple properties (ie. String) within the LogEntry works just fine. I just can’t seem to dig into the arrays to return the objects.
Any help would be much appreciated. I’m a bit of newbie with all of this. Thank you.
@nitramluap Thanks very much for your question, and my apologies for the delayed response.
I’m not sure if what you’re asking for is possible. A possible workaround is storing the array as a comma - or other character - separated string. Perhaps that might work.
What I’m asking for isn’t possible as you’ve guessed - and I found that out at WWDC 2018 when I got a chance to speak to one of the CoreData experts in the Labs. Prior to this I had actually worked out how to do it myself, doing exactly as you suggest with a searching String which reflects the items in the array.
I’ve since restructured my data model and have proper one-to-many relationships which prevents me having to do the work with code creating & updating strings. I never was confident enough to do this before as I thought my data model didn’t need to be too complicated, but after attending WWDC 2018 I made the leap and I wish I had done so earlier.
My data model is now a thing of beauty, with a dozen entities, each with their own attributes as well as interconnected relationships (one to one, one to many). I’m starting to get my head around CoreData. Version 3 of my app should be really nice!
The hardest part was writing the code to transform my old data model (single entity with many, many attributes) into multiple entities and still allow file importing to be backwards compatible!