2

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?

4
  • 1
    Possible duplicate of stackoverflow.com/questions/6104566/…. Commented Oct 6, 2018 at 10:47
  • Yes, that post somehow answers. I can't use it however since not familiar with Objective-C Commented Oct 6, 2018 at 11:05
  • Another possible solution - to create another Entity with possible array values and create relations between my object and new entities. Commented Oct 6, 2018 at 11:07
  • Yes. Avoid transformable attributes if you can. Commented Oct 6, 2018 at 11:22

1 Answer 1

2

Found an appropriate solution for CoreData

Don't put array as Transformable field in Entity

  1. Create separate Entity with a single field, use that entities instead of array.
  2. Create relation between main Entity and Array replacement Entity.
  3. During runtime: find CoreObject of array Entity using predicate:
NSPredicate(
    format: "%K = %@", #keyPath(YourArrayEntity.onlyField),
    matchingString
)
  1. Use predicates for searching your main entities
NSPredicate(
    format: "ANY %K == %@",
    #keyPath(YourMainEntity.relationToArrayEntity), 
    arrayEntityFoundInPreviousStep
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.