Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6159483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:08:27+00:00 2026-05-23T21:08:27+00:00

I’m working on a search feature in one of my Core Data based apps

  • 0

I’m working on a search feature in one of my Core Data based apps and I’m trying to gather everyone’s tips on search optimization to get it as fast as I possibly can. The search needs to be fast enough that it can deliver near-instantaneous results for database of 20,000+ objects.

What I’ve done so far (as far as optimization goes)

  • Implemented the technique shown in WWDC 2010 session 137, creating a keyword entity and creating a to-many relationship from my main object entities to it. The keyword entity’s name attribute is indexed, and keywords are created during the initial import procedure by splitting apart relevant strings in the main entities and normalizing them (stripped of case and diacritics)
  • Using >= and < binary comparators instead of BEGINSWITH, etc. My predicate format is:

SUBQUERY(keywords, $keyword, ($keyword.name >= $LB) AND ($keyword.name < $UB)).@count != 0

Where $LB is the lower bounds string and $UB is upper bounds. I create a compound AND predicate using this format and the array of search terms.

Right now, I’m executing a fetch once (when the user types the first letter) using a fetch batch size of about 20, and then narrowing down the search results using NSArray’s -filteredArrayUsingPredicate method as they continue typing. I also prefetch the keywords relationship because this is used to filter. The part that takes up the most time, obviously, is the initial fetch. There’s a noticeable delay of ~1-2s on a library of around 15,000 objects. Time profiling shows that it is indeed the fetch that is causing the delay:

http://cl.ly/3a1b2022452M2V323f2H

One other thing thats worth noting is that I have to fetch multiple entities for the results. All of the entities have a ranking attribute, but I can’t fetch more than one at once so I’m forced to fetch them separately, combine them into a single array, and then sort manually via -sortedArrayUsingDescriptors.

Any tips on how to speed this up would be greatly appreciated.

EDIT: Based on @ImHuntingWabbits’ suggestions:

After adding a KeywordFirstChar entity, my data model (simplified) would look like this:

new model

Now, the questions is how would I write a predicate for the Car entity that fetches based on KeywordFirstChar? The only thing I can think of would be this:

SUBQUERY(keywords, $keyword, $keyword.firstChar.char == %@) where %@ is the character to search for, but I don’t know how this would be much more efficient considering that it still has to enumerate over keywords, unless I misinterpreted the suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:08:27+00:00Added an answer on May 23, 2026 at 9:08 pm

    Your query is highly optimized, I think you’ve already taken a good number of steps. As far as the first character press, you’re doin it wrong.

    You’re still scanning 15k records for the first character hit, and likely matching a great number of them.

    You could further optimize it by indexing your keyword index, creating two new entities:

    • KeywordFirstChar
    • KeywordFirstTwoChars

    Both with a to-many relationship to the keywords they point to.

    if (searchPredicate.length == 1) {
        //search on KeywordFirstChar
    } else if (searchPredicate.length == 2) {
        //search on KeywordFirstTwoChars
    } else {
        //search on keyword
    }
    

    This way your table scan will go over max 26 and 676 rows respectively, which should be rather trivial. Just make sure the relationship is in the prefetched relationship key paths on the fetch request so you indeed get the data off disk.

    Edit (Object Retrieval):

    You can follow the relationship key path, so it would be something like this:

     [fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"keyword.sourceObject"]];
    

    Where keyword is the relationship to the Keyword entity, and sourceObject is the object that you want to eventually retrieve.

    Edit (Predicate):

    The predicate is essentially the same, just change the names to match the new entity (name might not map to name, instead firstChar or some other property).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.