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 7399743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:06:01+00:00 2026-05-29T04:06:01+00:00

This is more of a general design query with Core Data and how to

  • 0

This is more of a general design query with Core Data and how to design it so that it is efficient.

My app is a word game, with an sqlite3 database which holds a dictionary of words ranging from 2 – 28 letter words. The database has columns for the words and the size of the word.

The result I want to get (and the query I would like to design) is to fetch 10 random words of each size i.e. 10 random 2 letter words, 10 random 3 letter words and so on.

Additionally, I am trying to do this fetch up front in the AppDelegate as my application is loading.

I thought about doing this a few ways, but really need your opinions:

  1. fetch all letters of a certain size, and then get random records within the results. This means multiple fetches for each word length, and storing a lot of data.

  2. fetch random words using offsets on the index i.e. 0 – 20 = 2 letter words, 21 – 972 = 3 letter words

  3. fetch a random say 500 records based on a random index, and hope this contains at least 10 words of each length.

I am not sure how to design the query efficiently. I am trying to stick to the rule of a single fetch, and sort data after as much as possible.

Thanks in advance.
Pras.

  • 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-29T04:06:01+00:00Added an answer on May 29, 2026 at 4:06 am

    I think I have found my own answer through some testing.

    method (1) is obviously the simple way but far too resource intensive to fetch and store massive amount of data.

    method (3) is just silly.

    so I opted for method (2) with some newly acquired Core Data skills.

    The method is as follows:

    1. Setup my method to be run as a background thread
    2. Setup the NSFetchRequest, NSManagedObjectContext, and NSEntityDescription the standard way.
    3. Apply my predicate to the request e.g. all words of length 2
    4. Get the count of the values (using countForFetchRequest:) using the predicate
    5. Randomise the offset value (I know the offset for each length (hard coded), which I then add to (arc4random % count value) – making sure to check that my random offset value can rotate when nearing the max count e.g. if count = 22, and random offset = 21)
    6. Set fetch limit to return a small number of results e.g. 10.
    7. Execute the fetch request
    8. Store the results

    Although not completely random, the method above is quick, and returns 10 sequential values, from a random offset, and stores it to an NSMutableArray.

    Update: The actual code and detailed explanation can be found on my tutorial page: http://prasannaellanti.com/tutorials/

    Any other suggestions welcome.
    Pras.

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

Sidebar

Related Questions

This is more a general question but my particular case involves a ruby/rails app
Update: After some more reading I see that this problem is totally general, you
I asked this question in a more general design context before. Now, I'd like
This is more of a general design question, but it will be implemented in
This is more of a design question that others may have had similar experience
Although I'm programming in C++, this is more of a general question about design.
I suppose this question is more general than working with COM components. I have
this may be a more general opengl question. using OpenGL ES for 2d, and
I know this SO question, but it deals with the subject in more general
This is more an observation than a real question: MS-Access (and VBA in general)

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.