1

I’m using swift 4, Xcode 9, iOS 11

I am continuing to learn about CoreData via tutorials and Q&A posts, but I can’t seem to figure out how to answer this question.

I have a CoreData entity with one attribute and 12 relationships to different entities that each contain 1-3 attributes. This is because any given object within the main entity can have multiple additional details within each related entity.

I would like for the user to be able to search and match nearly anything within the database. My current attempt loops through the main entity with a FetchRequest and within each loop matches relationships. This works but is (as expected) somewhat slow.

I tried using a predicate laid out something like this: “name contains TERM OR ANY relation.name contains TERM......” But this grew into a long predicate and took 30+ seconds to load per fetchrequest. Much slower than without a predicate.

How do I essentially search the entire database more efficiently? Is there a better way to write the predicate? Is there a better way to design my data model?

Thank you for any guidance.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
Coltuxumab
  • 615
  • 5
  • 16

1 Answers1

0

1 month later, the solution was to split up the fetchRequests and add a predicate to each one individually. Furthermore, this allowed me to choose whether a specific related entity required BEGINSWITH[cd] or CONTAINS[cd].

To recap:

  • One long predicate lagged for ~30 seconds
  • A combined predicate was no better
  • Splitting the fetchRequests up and adding a small predicate to each one resulted in zero UI lag searching all 12 relationships.

Many thanks to Jon Rose for the suggestion.

Coltuxumab
  • 615
  • 5
  • 16