Reputation: 1452
I need to keep array of Strings in CoreData and later on use predicates for filtering data for that array.
According to this post field type Transformable (with custom class [String]) does the trick and I can properly save array of Strings: How to save Array to CoreData?
However, I can't use predicates for this fields. Specifically, my goal is to find all items where at least one item in saved array matches pattern.
So, back to question: is there a good way of storing array in CoreData so that I can apply predicates for it later on?
Upvotes: 2
Views: 1064
Reputation: 1452
Found an appropriate solution for CoreData
Don't put array as Transformable
field in Entity
Entity
with a single field, use that entities instead of array
.Entity
and Array replacement Entity
.CoreObject
of array Entity
using predicate:NSPredicate(
format: "%K = %@", #keyPath(YourArrayEntity.onlyField),
matchingString
)
NSPredicate(
format: "ANY %K == %@",
#keyPath(YourMainEntity.relationToArrayEntity),
arrayEntityFoundInPreviousStep
)
Upvotes: 2